home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / process.c < prev    next >
C/C++ Source or Header  |  1992-08-15  |  74KB  |  2,741 lines

  1. /* Asynchronous subprocess control for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <signal.h>
  22.  
  23. #include "config.h"
  24.  
  25. #ifdef VMS
  26. /* Prevent the file from being totally empty.  */
  27. static dummy () {}
  28. #endif
  29.  
  30. #ifdef subprocesses
  31. /* The entire file is within this conditional */
  32.  
  33. #include <stdio.h>
  34. #include <errno.h>
  35. #include <setjmp.h>
  36. #include <sys/types.h>        /* some typedefs are used in sys/file.h */
  37. #include <sys/file.h>
  38. #include <sys/stat.h>
  39.  
  40. #ifdef HAVE_SOCKETS    /* TCP connection support, if kernel can do it */
  41. #include <sys/socket.h>
  42. #include <netdb.h>
  43. #include <netinet/in.h>
  44. #endif /* HAVE_SOCKETS */
  45.  
  46. #if defined(BSD) || defined(STRIDE)
  47. #include <sys/ioctl.h>
  48. #if !defined (O_NDELAY) && defined (HAVE_PTYS)
  49. #include <fcntl.h>
  50. #endif /* HAVE_PTYS and no O_NDELAY */
  51. #endif /* BSD or STRIDE */
  52. #ifdef USG
  53. #ifndef NO_TERMIO
  54. #include <termio.h>
  55. #endif
  56. #include <fcntl.h>
  57. #endif /* USG */
  58.  
  59. #ifdef NEED_BSDTTY
  60. #include <sys/bsdtty.h>
  61. #endif
  62.  
  63. #ifdef NEED_TERMIOS
  64. #include <sys/termios.h>
  65. #endif
  66.  
  67. #ifdef TRITON88            /* To make emacs send C-c correctly in shell */
  68. #define TIOCGPGRP FIOGETOWN
  69. #endif
  70.  
  71. #ifdef HPUX
  72. #undef TIOCGPGRP
  73. #endif
  74.  
  75. /* Include time.h or sys/time.h or both.  */
  76. #include "gettime.h"
  77.  
  78. #if defined (HPUX) && defined (HAVE_PTYS)
  79. #include <sys/ptyio.h>
  80. #endif
  81.   
  82. #ifdef AIX
  83. #include <sys/pty.h>
  84. #include <unistd.h>
  85. #endif /* AIX */
  86.  
  87. #ifdef SYSV_PTYS
  88. #include <sys/tty.h>
  89. #include <sys/pty.h>
  90. #endif
  91.  
  92. #ifdef XENIX
  93. #undef TIOCGETC  /* Avoid confusing some conditionals that test this.  */
  94. #endif
  95.  
  96. #ifdef BROKEN_TIOCGETC
  97. #undef TIOCGETC
  98. #endif
  99.  
  100. #ifdef BROKEN_O_NONBLOCK
  101. #undef O_NONBLOCK
  102. #endif
  103.  
  104. #undef NULL
  105. #include "lisp.h"
  106. #include "window.h"
  107. #include "buffer.h"
  108. #include "process.h"
  109. #include "termhooks.h"
  110. #include "termopts.h"
  111. #include "commands.h"
  112. #include "dispextern.h"
  113.  
  114. Lisp_Object Qrun, Qstop, Qsignal, Qopen, Qclosed;
  115. extern Lisp_Object Qexit;
  116.  
  117. /* a process object is a network connection when its childp field is neither
  118.    Qt nor Qnil but is instead a string (name of foreign host we
  119.    are connected to + name of port we are connected to) */
  120.  
  121. #ifdef HAVE_SOCKETS
  122. #define NETCONN_P(p) (XGCTYPE (XPROCESS (p)->childp) == Lisp_String)
  123. #else
  124. #define NETCONN_P(p) 0
  125. #endif /* HAVE_SOCKETS */
  126.  
  127. /* Define SIGCHLD as an alias for SIGCLD.  There are many conditionals
  128.    testing SIGCHLD.  */
  129.  
  130. #if !defined (SIGCHLD) && defined (SIGCLD)
  131. #define SIGCHLD SIGCLD
  132. #endif /* SIGCLD */
  133.  
  134. #include "emacssignal.h"
  135.  
  136. /* Define the structure that the wait system call stores.
  137.    On many systems, there is a structure defined for this.
  138.    But on vanilla-ish USG systems there is not.  */
  139.  
  140. #ifndef WAITTYPE
  141. #if !defined (BSD) && !defined (UNIPLUS) && !defined (STRIDE) && !(defined (HPUX) && !defined (NOMULTIPLEJOBS)) && !defined (HAVE_WAIT_HEADER)
  142. #define WAITTYPE int
  143. #define WIFSTOPPED(w) ((w&0377) == 0177)
  144. #define WIFSIGNALED(w) ((w&0377) != 0177 && (w&~0377) == 0)
  145. #define WIFEXITED(w) ((w&0377) == 0)
  146. #define WRETCODE(w) (w >> 8)
  147. #define WSTOPSIG(w) (w >> 8)
  148. #define WCOREDUMP(w) ((w&0200) != 0)
  149. #define WTERMSIG(w) (w & 0377)
  150. #else
  151. #ifdef BSD4_1
  152. #include <wait.h>
  153. #else
  154. #include <sys/wait.h>
  155. #endif /* not BSD 4.1 */
  156.  
  157. #define WAITTYPE union wait
  158. #define WRETCODE(w) w.w_retcode
  159. #define WCOREDUMP(w) w.w_coredump
  160.  
  161. #ifdef HPUX
  162. /* HPUX version 7 has broken definitions of these.  */
  163. #undef WTERMSIG
  164. #undef WSTOPSIG
  165. #undef WIFSTOPPED
  166. #undef WIFSIGNALED
  167. #undef WIFEXITED
  168. #endif
  169.  
  170. #ifndef WTERMSIG
  171. #define WTERMSIG(w) w.w_termsig
  172. #endif
  173. #ifndef WSTOPSIG
  174. #define WSTOPSIG(w) w.w_stopsig
  175. #endif
  176. #ifndef WIFSTOPPED
  177. #define WIFSTOPPED(w) (WTERMSIG (w) == 0177)
  178. #endif
  179. #ifndef WIFSIGNALED
  180. #define WIFSIGNALED(w) (WTERMSIG (w) != 0177 && (WSTOPSIG (w)) == 0)
  181. #endif
  182. #ifndef WIFEXITED
  183. #define WIFEXITED(w) (WTERMSIG (w) == 0)
  184. #endif
  185. #endif /* BSD or UNIPLUS or STRIDE */
  186. #endif /* no WAITTYPE */
  187.  
  188. extern errno;
  189. extern sys_nerr;
  190. extern char *sys_errlist[];
  191.  
  192. #ifndef BSD4_1
  193. extern char *sys_siglist[];
  194. #else
  195. char *sys_siglist[] =
  196.   {
  197.     "bum signal!!",
  198.     "hangup",
  199.     "interrupt",
  200.     "quit",
  201.     "illegal instruction",
  202.     "trace trap",
  203.     "iot instruction",
  204.     "emt instruction",
  205.     "floating point exception",
  206.     "kill",
  207.     "bus error",
  208.     "segmentation violation",
  209.     "bad argument to system call",
  210.     "write on a pipe with no one to read it",
  211.     "alarm clock",
  212.     "software termination signal from kill",
  213.     "status signal",
  214.     "sendable stop signal not from tty",
  215.     "stop signal from tty",
  216.     "continue a stopped process",
  217.     "child status has changed",
  218.     "background read attempted from control tty",
  219.     "background write attempted from control tty",
  220.     "input record available at control tty",
  221.     "exceeded CPU time limit",
  222.     "exceeded file size limit"
  223.     };
  224. #endif
  225.  
  226. #ifdef vipc
  227.  
  228. #include "vipc.h"
  229. extern int comm_server;
  230. extern int net_listen_address;
  231. #endif /* vipc */
  232.  
  233. /* Communicate exit status of synch process to callproc.c.  */
  234. extern int synch_process_retcode;
  235. extern char *synch_process_death;
  236.  
  237. /* t means use pty, nil means use a pipe,
  238.    maybe other values to come.  */
  239. Lisp_Object Vprocess_connection_type;
  240.  
  241. #ifdef SKTPAIR
  242. #ifndef HAVE_SOCKETS
  243. #include <sys/socket.h>
  244. #endif
  245. #endif /* SKTPAIR */
  246.  
  247. /* Number of events of change of status of a process.  */
  248. int process_tick;
  249.  
  250. /* Number of events for which the user or sentinel has been notified.  */
  251. int update_tick;
  252.  
  253. int delete_exited_processes;
  254.  
  255. #ifdef FD_SET
  256. /* We could get this from param.h, but better not to depend on finding that.
  257.    And better not to risk that it might define other symbols used in this
  258.    file.  */
  259. #define MAXDESC 64
  260. #define SELECT_TYPE fd_set
  261. #else /* no FD_SET */
  262. #define MAXDESC 32
  263. #define SELECT_TYPE int
  264.  
  265. /* Define the macros to access a single-int bitmap of descriptors.  */
  266. #define FD_SET(n, p) (*(p) |= (1 << (n)))
  267. #define FD_CLR(n, p) (*(p) &= ~(1 << (n)))
  268. #define FD_ISSET(n, p) (*(p) & (1 << (n)))
  269. #define FD_ZERO(p) (*(p) = 0)
  270. #endif /* no FD_SET */
  271.  
  272. /* Mask of bits indicating the descriptors that we wait for input on */
  273.  
  274. SELECT_TYPE input_wait_mask;
  275.  
  276. /* Indexed by descriptor, gives the process (if any) for that descriptor */
  277. Lisp_Object chan_process[MAXDESC];
  278.  
  279. /* Alist of elements (NAME . PROCESS) */
  280. Lisp_Object Vprocess_alist;
  281.  
  282. Lisp_Object Qprocessp;
  283.  
  284. Lisp_Object get_process ();
  285.  
  286. /* Buffered-ahead input char from process, indexed by channel.
  287.    -1 means empty (no char is buffered).
  288.    Used on sys V where the only way to tell if there is any
  289.    output from the process is to read at least one char.
  290.    Always -1 on systems that support FIONREAD.  */
  291.  
  292. int proc_buffered_char[MAXDESC];
  293.  
  294. /* These variables hold the filter about to be run, and its args,
  295.    between read_process_output and run_filter.
  296.    Also used in exec_sentinel for sentinels.  */
  297. Lisp_Object this_filter;
  298. Lisp_Object filter_process, filter_string;
  299.  
  300. /* Compute the Lisp form of the process status, p->status,
  301.    from the numeric status that was returned by `wait'.  */
  302.  
  303. update_status (p)
  304.      struct Lisp_Process *p;
  305. {
  306.   union { int i; WAITTYPE wt; } u;
  307.   u.i = XFASTINT (p->raw_status_low) + (XFASTINT (p->raw_status_high) << 16);
  308.   p->status = status_convert (u.wt);
  309.   p->raw_status_low = Qnil;
  310.   p->raw_status_high = Qnil;
  311. }
  312.  
  313. /* Convert a process status word in Unix format
  314.    to the list that we use internally.  */
  315.  
  316. Lisp_Object
  317. status_convert (w)
  318.      WAITTYPE w;
  319. {
  320.   if (WIFSTOPPED (w))
  321.     return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
  322.   else if (WIFEXITED (w))
  323.     return Fcons (Qexit, Fcons (make_number (WRETCODE (w)),
  324.                 WCOREDUMP (w) ? Qt : Qnil));
  325.   else if (WIFSIGNALED (w))
  326.     return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
  327.                   WCOREDUMP (w) ? Qt : Qnil));
  328.   else
  329.     return Qrun;
  330. }
  331.  
  332. /* Given a status-list, extract the three pieces of information
  333.    and store them individually through the three pointers.  */
  334.  
  335. void
  336. decode_status (l, symbol, code, coredump)
  337.      Lisp_Object l;
  338.      Lisp_Object *symbol;
  339.      int *code;
  340.      int *coredump;
  341. {
  342.   Lisp_Object tem;
  343.  
  344.   if (XTYPE (l) == Lisp_Symbol)
  345.     {
  346.       *symbol = l;
  347.       *code = 0;
  348.       *coredump = 0;
  349.     }
  350.   else
  351.     {
  352.       *symbol = XCONS (l)->car;
  353.       tem = XCONS (l)->cdr;
  354.       *code = XFASTINT (XCONS (tem)->car);
  355.       tem = XFASTINT (XCONS (tem)->cdr);
  356.       *coredump = !NULL (tem);
  357.     }
  358. }
  359.  
  360. /* Return a string describing a process status list.  */
  361.  
  362. Lisp_Object 
  363. status_message (status)
  364.      Lisp_Object status;
  365. {
  366.   Lisp_Object symbol;
  367.   int code, coredump;
  368.   Lisp_Object string, string2;
  369.  
  370.   decode_status (status, &symbol, &code, &coredump);
  371.  
  372.   if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
  373.     {
  374.       string = build_string (code < NSIG ? sys_siglist[code] : "unknown");
  375.       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
  376.       XSTRING (string)->data[0] = DOWNCASE (XSTRING (string)->data[0]);
  377.       return concat2 (string, string2);
  378.     }
  379.   else if (EQ (symbol, Qexit))
  380.     {
  381.       if (code == 0)
  382.     return build_string ("finished\n");
  383.       string = Fint_to_string (make_number (code));
  384.       string2 = build_string (coredump ? " (core dumped)\n" : "\n");
  385.       return concat2 (build_string ("exited abnormally with code "),
  386.               concat2 (string, string2));
  387.     }
  388.   else
  389.     return Fcopy_sequence (Fsymbol_name (symbol));
  390. }
  391.  
  392. #ifdef HAVE_PTYS
  393.  
  394. /* Open an available pty, returning a file descriptor.
  395.    Return -1 on failure.
  396.    The file name of the terminal corresponding to the pty
  397.    is left in the variable pty_name.  */
  398.  
  399. char pty_name[24];
  400.  
  401. int
  402. allocate_pty ()
  403. {
  404.   struct stat stb;
  405.   register c, i;
  406.   int fd;
  407.  
  408. #ifdef PTY_ITERATION
  409.   PTY_ITERATION
  410. #else
  411.   for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
  412.     for (i = 0; i < 16; i++)
  413. #endif
  414.       {
  415. #ifdef PTY_NAME_SPRINTF
  416.     PTY_NAME_SPRINTF
  417. #else
  418. #ifdef HPUX
  419.     sprintf (pty_name, "/dev/ptym/pty%c%x", c, i);
  420. #else
  421. #ifdef RTU
  422.     sprintf (pty_name, "/dev/pty%x", i);
  423. #else
  424.     sprintf (pty_name, "/dev/pty%c%x", c, i);
  425. #endif /* not RTU */
  426. #endif /* not HPUX */
  427. #endif /* no PTY_NAME_SPRINTF */
  428.  
  429. #ifdef PTY_OPEN
  430.     PTY_OPEN;
  431. #else /* no PTY_OPEN */
  432. #ifndef IRIS
  433.     if (stat (pty_name, &stb) < 0)
  434.       return -1;
  435. #ifdef O_NONBLOCK
  436.     fd = open (pty_name, O_RDWR | O_NONBLOCK, 0);
  437. #else
  438.     fd = open (pty_name, O_RDWR | O_NDELAY, 0);
  439. #endif
  440. #else /* Unusual IRIS code */
  441.      fd = open ("/dev/ptc", O_RDWR | O_NDELAY, 0);
  442.      if (fd < 0)
  443.        return -1;
  444.     if (fstat (fd, &stb) < 0)
  445.       return -1;
  446. #endif /* IRIS */
  447. #endif /* no PTY_OPEN */
  448.  
  449.     if (fd >= 0)
  450.       {
  451.         /* check to make certain that both sides are available
  452.            this avoids a nasty yet stupid bug in rlogins */
  453. #ifdef PTY_TTY_NAME_SPRINTF
  454.         PTY_TTY_NAME_SPRINTF
  455. #else
  456.         /* In version 19, make these special cases use the macro above.  */
  457. #ifdef HPUX
  458.             sprintf (pty_name, "/dev/pty/tty%c%x", c, i);
  459. #else
  460. #ifdef RTU
  461.             sprintf (pty_name, "/dev/ttyp%x", i);
  462. #else
  463. #ifdef IRIS
  464.          sprintf (pty_name, "/dev/ttyq%d", minor (stb.st_rdev));
  465. #else
  466.             sprintf (pty_name, "/dev/tty%c%x", c, i);
  467. #endif /* not IRIS */
  468. #endif /* not RTU */
  469. #endif /* not HPUX */
  470. #endif /* no PTY_TTY_NAME_SPRINTF */
  471. #ifndef UNIPLUS
  472.         if (access (pty_name, 6) != 0)
  473.           {
  474.         close (fd);
  475. #ifndef IRIS
  476.         continue;
  477. #else
  478.         return -1;
  479. #endif /* IRIS */
  480.           }
  481. #endif /* not UNIPLUS */
  482.         setup_pty (fd);
  483.         return fd;
  484.       }
  485.       }
  486.   return -1;
  487. }
  488. #endif /* HAVE_PTYS */
  489.  
  490. Lisp_Object
  491. make_process (name)
  492.      Lisp_Object name;
  493. {
  494.   register Lisp_Object val, tem, name1;
  495.   register struct Lisp_Process *p;
  496.   char suffix[10];
  497.   register int i;
  498.  
  499.   /* size of process structure includes the vector header,
  500.      so deduct for that.  But struct Lisp_Vector includes the first
  501.      element, thus deducts too much, so add it back.  */
  502.   val = Fmake_vector (make_number ((sizeof (struct Lisp_Process)
  503.                     - sizeof (struct Lisp_Vector)
  504.                     + sizeof (Lisp_Object))
  505.                    / sizeof (Lisp_Object)),
  506.               Qnil);
  507.   XSETTYPE (val, Lisp_Process);
  508.  
  509.   p = XPROCESS (val);
  510.   XFASTINT (p->infd) = 0;
  511.   XFASTINT (p->outfd) = 0;
  512.   XFASTINT (p->pid) = 0;
  513.   XFASTINT (p->tick) = 0;
  514.   XFASTINT (p->update_tick) = 0;
  515.   p->raw_status_low = Qnil;
  516.   p->raw_status_high = Qnil;
  517.   p->status = Qrun;
  518.   p->mark = Fmake_marker ();
  519.  
  520.   /* If name is already in use, modify it until it is unused.  */
  521.  
  522.   name1 = name;
  523.   for (i = 1; ; i++)
  524.     {
  525.       tem = Fget_process (name1);
  526.       if (NULL (tem)) break;
  527.       sprintf (suffix, "<%d>", i);
  528.       name1 = concat2 (name, build_string (suffix));
  529.     }
  530.   name = name1;
  531.   p->name = name;
  532.   Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
  533.   return val;
  534. }
  535.  
  536. remove_process (proc)
  537.      register Lisp_Object proc;
  538. {
  539.   register Lisp_Object pair;
  540.  
  541.   pair = Frassq (proc, Vprocess_alist);
  542.   Vprocess_alist = Fdelq (pair, Vprocess_alist);
  543.   Fset_marker (XPROCESS (proc)->mark, Qnil, Qnil);
  544.  
  545.   deactivate_process (proc);
  546. }
  547.  
  548. DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
  549.   "Return t if OBJECT is a process.")
  550.   (obj)
  551.      Lisp_Object obj;
  552. {
  553.   return XTYPE (obj) == Lisp_Process ? Qt : Qnil;
  554. }
  555.  
  556. DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
  557.   "Return the process named NAME, or nil if there is none.")
  558.   (name)
  559.      register Lisp_Object name;
  560. {
  561.   if (XTYPE (name) == Lisp_Process)
  562.     return name;
  563.   CHECK_STRING (name, 0);
  564.   return Fcdr (Fassoc (name, Vprocess_alist));
  565. }
  566.  
  567. DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
  568.   "Return the (or, a) process associated with BUFFER.\n\
  569. BUFFER may be a buffer or the name of one.")
  570.   (name)
  571.      register Lisp_Object name;
  572. {
  573.   register Lisp_Object buf, tail, proc;
  574.  
  575.   if (NULL (name)) return Qnil;
  576.   buf = Fget_buffer (name);
  577.   if (NULL (buf)) return Qnil;
  578.  
  579.   for (tail = Vprocess_alist; !NULL (tail); tail = Fcdr (tail))
  580.     {
  581.       proc = Fcdr (Fcar (tail));
  582.       if (XTYPE (proc) == Lisp_Process && EQ (XPROCESS (proc)->buffer, buf))
  583.     return proc;
  584.     }
  585.   return Qnil;
  586. }
  587.  
  588. /* This is how commands for the user decode process arguments */
  589.  
  590. Lisp_Object
  591. get_process (name)
  592.      register Lisp_Object name;
  593. {
  594.   register Lisp_Object proc;
  595.   if (NULL (name))
  596.     proc = Fget_buffer_process (Fcurrent_buffer ());
  597.   else
  598.     {
  599.       proc = Fget_process (name);
  600.       if (NULL (proc))
  601.     proc = Fget_buffer_process (Fget_buffer (name));
  602.     }
  603.  
  604.   if (!NULL (proc))
  605.     return proc;
  606.  
  607.   if (NULL (name))
  608.     error ("Current buffer has no process");
  609.   else
  610.     error ("Process %s does not exist", XSTRING (name)->data);
  611.   /* NOTREACHED */
  612. }
  613.  
  614. DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
  615.   "Delete PROCESS: kill it and forget about it immediately.\n\
  616. PROCESS may be a process or the name of one, or a buffer name.")
  617.   (proc)
  618.      register Lisp_Object proc;
  619. {
  620.   proc = get_process (proc);
  621.   XPROCESS (proc)->raw_status_low = Qnil;
  622.   XPROCESS (proc)->raw_status_high = Qnil;
  623.   if (NETCONN_P (proc))
  624.     {
  625.       XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (0), Qnil));
  626.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  627.     }
  628.   else if (XFASTINT (XPROCESS (proc)->infd))
  629.     {
  630.       Fkill_process (proc, Qnil);
  631.       /* Do this now, since remove_process will make sigchld_handler do nothing.  */
  632.       XPROCESS (proc)->status
  633.     = Fcons (Qsignal, Fcons (make_number (SIGKILL), Qnil));
  634.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  635.       status_notify ();
  636.     }
  637.   remove_process (proc);
  638.   return Qnil;
  639. }
  640.  
  641. DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
  642.   "Return the status of PROCESS: a symbol, one of these:\n\
  643. run  -- for a process that is running.\n\
  644. stop -- for a process stopped but continuable.\n\
  645. exit -- for a process that has exited.\n\
  646. signal -- for a process that has got a fatal signal.\n\
  647. open -- for a network stream connection that is open.\n\
  648. closed -- for a network stream connection that is closed.\n\
  649. nil -- if arg is a process name and no such process exists.")
  650. /* command -- for a command channel opened to Emacs by another process.\n\
  651.    external -- for an i/o channel opened to Emacs by another process.\n\  */
  652.   (proc)
  653.      register Lisp_Object proc;
  654. {
  655.   register struct Lisp_Process *p;
  656.   register Lisp_Object status;
  657.   proc = Fget_process (proc);
  658.   if (NULL (proc))
  659.     return proc;
  660.   p = XPROCESS (proc);
  661.   if (!NULL (p->raw_status_low))
  662.     update_status (p);
  663.   status = p->status;
  664.   if (XTYPE (status) == Lisp_Cons)
  665.     status = XCONS (status)->car;
  666.   if (NETCONN_P (proc))
  667.     {
  668.       if (EQ (status, Qrun))
  669.     status = Qopen;
  670.       else if (EQ (status, Qexit))
  671.     status = Qclosed;
  672.     }
  673.   return status;
  674. }
  675.  
  676. DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
  677.        1, 1, 0,
  678.   "Return the exit status of PROCESS or the signal number that killed it.\n\
  679. If PROCESS has not yet exited or died, return 0.\n\
  680. If PROCESS is a net connection that was closed remotely, return 256.")
  681.   (proc)
  682.      register Lisp_Object proc;
  683. {
  684.   CHECK_PROCESS (proc, 0);
  685.   if (!NULL (XPROCESS (proc)->raw_status_low))
  686.     update_status (XPROCESS (proc));
  687.   if (XTYPE (XPROCESS (proc)->status) == Lisp_Cons)
  688.     return XCONS (XCONS (XPROCESS (proc)->status)->cdr)->car;
  689.   return make_number (0);
  690. }
  691.  
  692. DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
  693.   "Return the process id of PROCESS.\n\
  694. This is the pid of the Unix process which PROCESS uses or talks to.\n\
  695. For a network connection, this value is nil.")
  696.   (proc)
  697.      register Lisp_Object proc;
  698. {
  699.   CHECK_PROCESS (proc, 0);
  700.   return XPROCESS (proc)->pid;
  701. }
  702.  
  703. DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
  704.   "Return the name of PROCESS, as a string.\n\
  705. This is the name of the program invoked in PROCESS,\n\
  706. possibly modified to make it unique among process names.")
  707.   (proc)
  708.      register Lisp_Object proc;
  709. {
  710.   CHECK_PROCESS (proc, 0);
  711.   return XPROCESS (proc)->name;
  712. }
  713.  
  714. DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
  715.   "Return the command that was executed to start PROCESS.\n\
  716. This is a list of strings, the first string being the program executed\n\
  717. and the rest of the strings being the arguments given to it.\n\
  718. For a non-child channel, this is nil.")
  719.   (proc)
  720.      register Lisp_Object proc;
  721. {
  722.   CHECK_PROCESS (proc, 0);
  723.   return XPROCESS (proc)->command;
  724. }
  725.  
  726. DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
  727.   2, 2, 0,
  728.   "Set buffer associated with PROCESS to BUFFER (a buffer, or nil).")
  729.   (proc, buffer)
  730.      register Lisp_Object proc, buffer;
  731. {
  732.   CHECK_PROCESS (proc, 0);
  733.   if (!NULL (buffer))
  734.     CHECK_BUFFER (buffer, 1);
  735.   XPROCESS (proc)->buffer = buffer;
  736.   return buffer;
  737. }
  738.  
  739. DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
  740.   1, 1, 0,
  741.   "Return the buffer PROCESS is associated with.\n\
  742. Output from PROCESS is inserted in this buffer\n\
  743. unless PROCESS has a filter.")
  744.   (proc)
  745.      register Lisp_Object proc;
  746. {
  747.   CHECK_PROCESS (proc, 0);
  748.   return XPROCESS (proc)->buffer;
  749. }
  750.  
  751. DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
  752.   1, 1, 0,
  753.   "Return the marker for the end of the last output from PROCESS.")
  754.   (proc)
  755.      register Lisp_Object proc;
  756. {
  757.   CHECK_PROCESS (proc, 0);
  758.   return XPROCESS (proc)->mark;
  759. }
  760.  
  761. DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
  762.   2, 2, 0,
  763.   "Give PROCESS the filter function FILTER; nil means no filter.\n\
  764. When a process has a filter, each time it does output\n\
  765. the entire string of output is passed to the filter.\n\
  766. The filter gets two arguments: the process and the string of output.\n\
  767. If the process has a filter, its buffer is not used for output.")
  768.   (proc, filter)
  769.      register Lisp_Object proc, filter;
  770. {
  771.   CHECK_PROCESS (proc, 0);
  772.   XPROCESS (proc)->filter = filter;
  773.   return filter;
  774. }
  775.  
  776. DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
  777.   1, 1, 0,
  778.   "Returns the filter function of PROCESS; nil if none.\n\
  779. See set-process-filter for more info on filter functions.")
  780.   (proc)
  781.      register Lisp_Object proc;
  782. {
  783.   CHECK_PROCESS (proc, 0);
  784.   return XPROCESS (proc)->filter;
  785. }
  786.  
  787. DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
  788.   2, 2, 0,
  789.   "Give PROCESS the sentinel SENTINEL; nil for none.\n\
  790. The sentinel is called as a function when the process changes state.\n\
  791. It gets two arguments: the process, and a string describing the change.")
  792.   (proc, sentinel)
  793.      register Lisp_Object proc, sentinel;
  794. {
  795.   CHECK_PROCESS (proc, 0);
  796.   XPROCESS (proc)->sentinel = sentinel;
  797.   return sentinel;
  798. }
  799.  
  800. DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
  801.   1, 1, 0,
  802.   "Return the sentinel of PROCESS; nil if none.\n\
  803. See set-process-sentinel for more info on sentinels.")
  804.   (proc)
  805.      register Lisp_Object proc;
  806. {
  807.   CHECK_PROCESS (proc, 0);
  808.   return XPROCESS (proc)->sentinel;
  809. }
  810.  
  811. DEFUN ("process-kill-without-query", Fprocess_kill_without_query,
  812.   Sprocess_kill_without_query, 1, 2, 0,
  813.   "Say no query needed if PROCESS is running when Emacs is exited.\n\
  814. Optional second argument if non-nil says to require a query.\n\
  815. Value is t if a query was formerly required.")
  816.   (proc, value)
  817.      register Lisp_Object proc, value;
  818. {
  819.   Lisp_Object tem;
  820.   CHECK_PROCESS (proc, 0);
  821.   tem = XPROCESS (proc)->kill_without_query;
  822.   XPROCESS (proc)->kill_without_query = Fnull (value);
  823.   return Fnull (tem);
  824. }
  825.  
  826. Lisp_Object
  827. list_processes_1 ()
  828. {
  829.   register Lisp_Object tail, tem;
  830.   Lisp_Object proc, minspace, tem1;
  831.   register struct buffer *old = current_buffer;
  832.   register struct Lisp_Process *p;
  833.   register int state;
  834.   char tembuf[80];
  835.  
  836.   XFASTINT (minspace) = 1;
  837.  
  838.   set_buffer_internal (XBUFFER (Vstandard_output));
  839.   Fbuffer_flush_undo (Vstandard_output);
  840.  
  841.   current_buffer->truncate_lines = Qt;
  842.  
  843.   write_string ("\
  844. Proc         Status   Buffer         Command\n\
  845. ----         ------   ------         -------\n", -1);
  846.  
  847.   for (tail = Vprocess_alist; !NULL (tail); tail = Fcdr (tail))
  848.     {
  849.       Lisp_Object symbol;
  850.  
  851.       proc = Fcdr (Fcar (tail));
  852.       p = XPROCESS (proc);
  853.       if (NULL (p->childp))
  854.     continue;
  855.  
  856.       Finsert (1, &p->name);
  857.       Findent_to (make_number (13), minspace);
  858.  
  859.       if (!NULL (p->raw_status_low))
  860.     update_status (p);
  861.       symbol = p->status;
  862.       if (XTYPE (p->status) == Lisp_Cons)
  863.     symbol = XCONS (p->status)->car;
  864.  
  865.       if (EQ (symbol, Qsignal))
  866.     {
  867.       Lisp_Object tem;
  868.       tem = Fcar (Fcdr (p->status));
  869.       if (XINT (tem) < NSIG)
  870.         write_string (sys_siglist [XINT (tem)], -1);
  871.       else
  872.         Fprinc (symbol, Qnil);
  873.     }
  874.       else if (NETCONN_P (proc))
  875.     {
  876.       if (EQ (symbol, Qrun))
  877.         write_string ("open", -1);
  878.       else if (EQ (symbol, Qexit))
  879.         write_string ("closed", -1);
  880.       else
  881.         Fprinc (symbol, Qnil);
  882.     }
  883.       else
  884.     Fprinc (symbol, Qnil);
  885.  
  886.       if (EQ (symbol, Qexit))
  887.     {
  888.       Lisp_Object tem;
  889.       tem = Fcar (Fcdr (p->status));
  890.       if (XFASTINT (tem))
  891.         {
  892.           sprintf (tembuf, " %d", XFASTINT (tem));
  893.           write_string (tembuf, -1);
  894.         }
  895.     }
  896.  
  897.       if (EQ (symbol, Qsignal) || EQ (symbol, Qexit))
  898.     remove_process (proc);
  899.  
  900.       Findent_to (make_number (22), minspace);
  901.       if (NULL (p->buffer))
  902.     InsStr ("(none)");
  903.       else if (NULL (XBUFFER (p->buffer)->name))
  904.     InsStr ("(Killed)");
  905.       else
  906.     Finsert (1, &XBUFFER (p->buffer)->name);
  907.  
  908.       Findent_to (make_number (37), minspace);
  909.  
  910.       if (NETCONN_P (proc))
  911.         {
  912.       sprintf (tembuf, "(network stream connection to %s)\n",
  913.            XSTRING (p->childp)->data);
  914.       InsStr (tembuf);
  915.         }
  916.       else 
  917.     {
  918.       tem = p->command;
  919.       while (1)
  920.         {
  921.           tem1 = Fcar (tem);
  922.           Finsert (1, &tem1);
  923.           tem = Fcdr (tem);
  924.           if (NULL (tem))
  925.         break;
  926.           InsStr (" ");
  927.         }
  928.       InsStr ("\n");
  929.        }
  930.     }
  931.  
  932.   return Qnil;
  933. }
  934.  
  935. DEFUN ("list-processes", Flist_processes, Slist_processes, 0, 0, "",
  936.   "Display a list of all processes.\n\
  937. \(Any processes listed as Exited or Signaled are actually eliminated\n\
  938. after the listing is made.)")
  939.   ()
  940. {
  941.   internal_with_output_to_temp_buffer ("*Process List*",
  942.                        list_processes_1, Qnil);
  943.   return Qnil;
  944. }
  945.  
  946. DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
  947.   "Return a list of all processes.")
  948.   ()
  949. {
  950.   return Fmapcar (Qcdr, Vprocess_alist);
  951. }
  952.  
  953. DEFUN ("start-process", Fstart_process, Sstart_process, 3, MANY, 0,
  954.   "Start a program in a subprocess.  Return the process object for it.\n\
  955. Args are NAME BUFFER PROGRAM &rest PROGRAM-ARGS\n\
  956. NAME is name for process.  It is modified if necessary to make it unique.\n\
  957. BUFFER is the buffer or (buffer-name) to associate with the process.\n\
  958.  Process output goes at end of that buffer, unless you specify\n\
  959.  an output stream or filter function to handle the output.\n\
  960.  BUFFER may be also nil, meaning that this process is not associated\n\
  961.  with any buffer\n\
  962. Third arg is program file name.  It is searched for as in the shell.\n\
  963. Remaining arguments are strings to give program as arguments.")
  964.   (nargs, args)
  965.      int nargs;
  966.      register Lisp_Object *args;
  967. {
  968.   Lisp_Object buffer, name, program, proc, tem;
  969.   register unsigned char **new_argv;
  970.   register int i;
  971.  
  972.   buffer = args[1];
  973.   if (!NULL (buffer))
  974.     buffer = Fget_buffer_create (buffer);
  975.  
  976.   name = args[0];
  977.   CHECK_STRING (name, 0);
  978.  
  979.   program = args[2];
  980.  
  981.   CHECK_STRING (program, 2);
  982.  
  983.   new_argv = (unsigned char **) alloca ((nargs - 1) * sizeof (char *));
  984.  
  985.   for (i = 3; i < nargs; i++)
  986.     {
  987.       tem = args[i];
  988.       CHECK_STRING (tem, i);
  989.       new_argv[i - 2] = XSTRING (tem)->data;
  990.     }
  991.   new_argv[i - 2] = 0;
  992.   new_argv[0] = XSTRING (program)->data;
  993.  
  994.   /* If program file name is not absolute, search our path for it */
  995.   if (new_argv[0][0] != '/')
  996.     {
  997.       tem = Qnil;
  998.       openp (Vexec_path, program, "", &tem, 1);
  999.       if (NULL (tem))
  1000.     report_file_error ("Searching for program", Fcons (program, Qnil));
  1001.       new_argv[0] = XSTRING (tem)->data;
  1002.     }
  1003.  
  1004.   proc = make_process (name);
  1005.  
  1006.   XPROCESS (proc)->childp = Qt;
  1007.   XPROCESS (proc)->command_channel_p = Qnil;
  1008.   XPROCESS (proc)->buffer = buffer;
  1009.   XPROCESS (proc)->sentinel = Qnil;
  1010.   XPROCESS (proc)->filter = Qnil;
  1011.   XPROCESS (proc)->command = Flist (nargs - 2, args + 2);
  1012.  
  1013.   create_process (proc, new_argv);
  1014.  
  1015.   return proc;
  1016. }
  1017.  
  1018. create_process_1 (signo)
  1019.      int signo;
  1020. {
  1021. #ifdef USG
  1022.   /* USG systems forget handlers when they are used;
  1023.      must reestablish each time */
  1024.   signal (signo, create_process_1);
  1025. #endif /* USG */
  1026. }
  1027.  
  1028. #if 0  /* This doesn't work; see the note before sigchld_handler.  */
  1029. #ifdef USG
  1030. #ifdef SIGCHLD
  1031. /* Mimic blocking of signals on system V, which doesn't really have it.  */
  1032.  
  1033. /* Nonzero means we got a SIGCHLD when it was supposed to be blocked.  */
  1034. int sigchld_deferred;
  1035.  
  1036. create_process_sigchld ()
  1037. {
  1038.   signal (SIGCHLD, create_process_sigchld);
  1039.  
  1040.   sigchld_deferred = 1;
  1041. }
  1042. #endif
  1043. #endif
  1044. #endif
  1045.  
  1046. create_process (process, new_argv)
  1047.      Lisp_Object process;
  1048.      char **new_argv;
  1049. {
  1050.   int pid, inchannel, outchannel, forkin, forkout;
  1051.   int sv[2];
  1052. #ifdef SIGCHLD
  1053.   int (*sigchld)();
  1054. #endif
  1055.   char **env;
  1056.   int pty_flag = 0;
  1057.   extern char **environ;
  1058.  
  1059. #ifdef MAINTAIN_ENVIRONMENT
  1060.   env = (char **) alloca (size_of_current_environ ());
  1061.   get_current_environ (env);
  1062. #else
  1063.   env = environ;
  1064. #endif /* MAINTAIN_ENVIRONMENT */
  1065.  
  1066.   inchannel = outchannel = -1;
  1067.  
  1068. #ifdef HAVE_PTYS
  1069.   if (EQ (Vprocess_connection_type, Qt))
  1070.     outchannel = inchannel = allocate_pty ();
  1071.  
  1072.   if (inchannel >= 0)
  1073.     {
  1074. #ifndef USG
  1075.       /* On USG systems it does not work to open
  1076.      the pty's tty here and then close and reopen it in the child.  */
  1077. #ifdef O_NOCTTY
  1078.       /* Don't let this terminal become our controlling terminal
  1079.      (in case we don't have one).  */
  1080.       forkout = forkin = open (pty_name, O_RDWR | O_NOCTTY, 0);
  1081. #else
  1082.       forkout = forkin = open (pty_name, O_RDWR, 0);
  1083. #endif
  1084.       if (forkin < 0)
  1085.     report_file_error ("Opening pty", Qnil);
  1086. #else
  1087.       forkin = forkout = -1;
  1088. #endif
  1089.       pty_flag = 1;
  1090.     }
  1091.   else
  1092. #endif /* HAVE_PTYS */
  1093. #ifdef SKTPAIR
  1094.     {
  1095.       if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv) < 0)
  1096.     report_file_error ("Opening socketpair", Qnil);
  1097.       outchannel = inchannel = sv[0];
  1098.       forkout = forkin = sv[1];
  1099.     }
  1100. #else /* not SKTPAIR */
  1101.     {
  1102.       int temp;
  1103.       temp = pipe (sv);
  1104.       if (temp < 0) goto io_failure;
  1105.       inchannel = sv[0];
  1106.       forkout = sv[1];
  1107.       temp = pipe (sv);
  1108.       if (temp < 0) goto io_failure;
  1109.       outchannel = sv[1];
  1110.       forkin = sv[0];
  1111.     }
  1112. #endif /* not SKTPAIR */
  1113.  
  1114. #if 0
  1115.   /* Replaced by close_process_descs */
  1116.   set_exclusive_use (inchannel);
  1117.   set_exclusive_use (outchannel);
  1118. #endif
  1119.  
  1120. /* Stride people say it's a mystery why this is needed
  1121.    as well as the O_NDELAY, but that it fails without this.  */
  1122. #ifdef STRIDE
  1123.   {
  1124.     int one = 1;
  1125.     ioctl (inchannel, FIONBIO, &one);
  1126.   }
  1127. #endif
  1128.  
  1129. #ifdef O_NONBLOCK
  1130.   fcntl (inchannel, F_SETFL, O_NONBLOCK);
  1131. #else
  1132. #ifdef O_NDELAY
  1133.   fcntl (inchannel, F_SETFL, O_NDELAY);
  1134. #endif
  1135. #endif
  1136.  
  1137.   XFASTINT (XPROCESS (process)->infd) = inchannel;
  1138.   XFASTINT (XPROCESS (process)->outfd) = outchannel;
  1139.   /* Record the tty descriptor used in the subprocess.  */
  1140. #ifdef SYSV4_PTYS
  1141.   /* On system V.4, if using a pty, we need to keep a descriptor
  1142.      for the tty that the inferior uses, in order to get the pgrp.
  1143.      If this uses too many descriptors, we could instead save the tty name
  1144.      and reopen it to send signals.  */
  1145.   if (pty_flag)
  1146.     {
  1147.       int temp = dup (forkin);
  1148.       if (temp < 0) goto io_failure;
  1149.       XFASTINT (XPROCESS (process)->subtty) = temp;
  1150.     }
  1151.   else
  1152. #endif
  1153.     XPROCESS (process)->subtty = Qnil;
  1154.   XPROCESS (process)->pty_flag = (pty_flag ? Qt : Qnil);
  1155.   XPROCESS (process)->status = Qrun;
  1156.   /* Record this as an active process, with its channels.
  1157.      As a result, child_setup will close Emacs's side of the pipes.  */
  1158.   chan_process[inchannel] = process;
  1159.  
  1160.   /* Delay interrupts until we have a chance to store
  1161.      the new fork's pid in its process structure */
  1162. #ifdef SIGCHLD
  1163. #ifdef BSD4_1
  1164.   sighold (SIGCHLD);
  1165. #else /* not BSD4_1 */
  1166. #ifdef HPUX
  1167.   sigsetmask (sigmask (SIGCHLD));
  1168. #else /* not HPUX */
  1169. #if defined (BSD) || defined (UNIPLUS)
  1170.   sigsetmask (sigmask (SIGCHLD));
  1171. #else /* ordinary USG */
  1172. #if 0
  1173.   sigchld_deferred = 0;
  1174.   sigchld = (int (*)()) signal (SIGCHLD, create_process_sigchld);
  1175. #endif
  1176. #endif /* ordinary USG */
  1177. #endif /* not HPUX */
  1178. #endif /* not BSD4_1 */
  1179. #endif /* SIGCHLD */
  1180.  
  1181.   /* Until we store the proper pid, enable sigchld_handler
  1182.      to recognize an unknown pid as standing for this process.  */
  1183.   XSETINT (XPROCESS (process)->pid, -1);
  1184.   /* Turn on the bit for our input from this process now,
  1185.      so that even if the process terminates very soon,
  1186.      we can clear the bit properly on termination.
  1187.      If fork fails, remove_process will clear the bit.  */
  1188.   FD_SET (inchannel, &input_wait_mask);
  1189.  
  1190. #ifdef AMIGA
  1191.   {
  1192.     register unsigned char *temp;
  1193.  
  1194.     if (XTYPE (current_buffer->directory) == Lisp_String)
  1195.       {
  1196.     register int i;
  1197.  
  1198.     i = XSTRING (current_buffer->directory)->size;
  1199.     temp = (unsigned char *) alloca (i + 1);
  1200.     bcopy (XSTRING (current_buffer->directory)->data, temp, i);
  1201.     temp[i] = 0;
  1202.       }
  1203.     pid = exec(new_argv[0], new_argv, forkin, forkout, temp,
  1204.            amiga_process_stack_size);
  1205.   }
  1206. #else /* not AMIGA */
  1207.   {
  1208.     /* child_setup must clobber environ on systems with true vfork.
  1209.        Protect it from permanent change.  */
  1210.     char **save_environ = environ;
  1211.  
  1212.     pid = vfork ();
  1213.     if (pid == 0)
  1214.       {
  1215.     int xforkin = forkin;
  1216.     int xforkout = forkout;
  1217.  
  1218. #if 0 /* This was probably a mistake--it duplicates code later on,
  1219.      but fails to handle all the cases.  */
  1220.     /* Make SIGCHLD work again in the child.  */
  1221.     sigsetmask (SIGEMPTYMASK);
  1222. #endif
  1223.  
  1224.     /* Make the pty be the controlling terminal of the process.  */
  1225. #ifdef HAVE_PTYS
  1226.     /* First, disconnect its current controlling terminal.  */
  1227. #ifdef HAVE_SETSID
  1228.     setsid ();
  1229. #else /* not HAVE_SETSID */
  1230. #ifdef USG
  1231.     /* It's very important to call setpgrp() here and no time
  1232.        afterwards.  Otherwise, we lose our controlling tty which
  1233.        is set when we open the pty. */
  1234.     setpgrp ();
  1235. #endif /* USG */
  1236. #endif /* not HAVE_SETSID */
  1237. #ifdef TIOCNOTTY
  1238.     /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
  1239.        can do TIOCSPGRP only to the process's controlling tty.  */
  1240.     if (pty_flag)
  1241.       {
  1242.         /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here? 
  1243.            I can't test it since I don't have 4.3.  */
  1244.         int j = open ("/dev/tty", O_RDWR, 0);
  1245.         ioctl (j, TIOCNOTTY, 0);
  1246.         close (j);
  1247. #ifndef USG
  1248.         /* In order to get a controlling terminal on some versions
  1249.            of BSD, it is necessary to put the process in pgrp 0
  1250.            before it opens the terminal.  */
  1251.         setpgrp (0, 0);
  1252. #endif
  1253.       }
  1254. #endif /* TIOCNOTTY */
  1255.  
  1256. #if !defined (RTU) && !defined (UNIPLUS)
  1257. /*** There is a suggestion that this ought to be a
  1258.      conditional on TIOCSPGRP.  */
  1259.     /* Now close the pty (if we had it open) and reopen it.
  1260.        This makes the pty the controlling terminal of the subprocess.  */
  1261.     if (pty_flag)
  1262.       {
  1263.         /* I wonder if close (open (pty_name, ...)) would work?  */
  1264.         if (xforkin >= 0)
  1265.           close (xforkin);
  1266.         xforkout = xforkin = open (pty_name, O_RDWR, 0);
  1267.  
  1268.         if (xforkin < 0)
  1269.           abort ();
  1270.       }
  1271. #endif /* not UNIPLUS and not RTU */
  1272. #ifdef SETUP_SLAVE_PTY
  1273.     SETUP_SLAVE_PTY;
  1274. #endif /* SETUP_SLAVE_PTY */
  1275. #ifdef AIX
  1276.     /* On AIX, we've disabled SIGHUP above once we start a child on a pty.
  1277.        Now reenable it in the child, so it will die when we want it to.  */
  1278.     if (pty_flag)
  1279.       signal (SIGHUP, SIG_DFL);
  1280. #endif
  1281. #endif /* HAVE_PTYS */
  1282. #ifdef SIGCHLD
  1283. #ifdef BSD4_1
  1284.     sigrelse (SIGCHLD);
  1285. #else /* not BSD4_1 */
  1286. #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
  1287.     sigsetmask (SIGEMPTYMASK);
  1288. #else /* ordinary USG */
  1289. #if 0
  1290.     signal (SIGCHLD, sigchld);
  1291. #endif
  1292. #endif /* ordinary USG */
  1293. #endif /* not BSD4_1 */
  1294. #endif /* SIGCHLD */
  1295.     child_setup_tty (xforkout);
  1296.     child_setup (xforkin, xforkout, xforkout, new_argv, env);
  1297.       }
  1298.     environ = save_environ;
  1299.   }
  1300. #endif /* not AMIGA */
  1301.  
  1302.   if (pid < 0)
  1303.     {
  1304.       remove_process (process);
  1305.       report_file_error ("Doing vfork", Qnil);
  1306.     }
  1307.  
  1308.   XFASTINT (XPROCESS (process)->pid) = pid;
  1309.  
  1310. #ifndef AMIGA
  1311.   /* If the subfork execv fails, and it exits,
  1312.      this close hangs.  I don't know why.
  1313.      So have an interrupt jar it loose.  */
  1314.   stop_polling ();
  1315.   signal (SIGALRM, create_process_1);
  1316.   alarm (1);
  1317.   if (forkin >= 0)
  1318.     close (forkin);
  1319.   alarm (0);
  1320.   start_polling ();
  1321.   if (forkin != forkout && forkout >= 0)
  1322.     close (forkout);
  1323. #endif
  1324.  
  1325. #ifdef SIGCHLD
  1326. #ifdef BSD4_1
  1327.   sigrelse (SIGCHLD);
  1328. #else /* not BSD4_1 */
  1329. #if defined (BSD) || defined (UNIPLUS) || defined (HPUX)
  1330.   sigsetmask (SIGEMPTYMASK);
  1331. #else /* ordinary USG */
  1332. #if 0
  1333.   signal (SIGCHLD, sigchld);
  1334.   /* Now really handle any of these signals
  1335.      that came in during this function.  */
  1336.   if (sigchld_deferred)
  1337.     kill (getpid (), SIGCHLD);
  1338. #endif
  1339. #endif /* ordinary USG */
  1340. #endif /* not BSD4_1 */
  1341. #endif /* SIGCHLD */
  1342.   return;
  1343.  
  1344. io_failure:
  1345.   {
  1346.     int temp = errno;
  1347.     close (forkin);
  1348.     close (forkout);
  1349.     close (inchannel);
  1350.     close (outchannel);
  1351.     errno = temp;
  1352.     report_file_error ("Opening pty or pipe", Qnil);
  1353.   }
  1354. }
  1355.  
  1356. #ifdef HAVE_SOCKETS
  1357.  
  1358. /* open a TCP network connection to a given HOST/SERVICE.  Treated
  1359.    exactly like a normal process when reading and writing.  Only
  1360.    differences are in status display and process deletion.  A network
  1361.    connection has no PID; you cannot signal it.  All you can do is
  1362.    deactivate and close it via delete-process */
  1363.  
  1364. DEFUN ("open-network-stream", Fopen_network_stream, Sopen_network_stream, 
  1365.        4, 4, 0, 
  1366.   "Open a TCP connection for a service to a host.\n\
  1367. Returns a subprocess-object to represent the connection.\n\
  1368. Input and output work as for subprocesses; `delete-process' closes it.\n\
  1369. Args are NAME BUFFER HOST SERVICE.\n\
  1370. NAME is name for process.  It is modified if necessary to make it unique.\n\
  1371. BUFFER is the buffer (or buffer-name) to associate with the process.\n\
  1372.  Process output goes at end of that buffer, unless you specify\n\
  1373.  an output stream or filter function to handle the output.\n\
  1374.  BUFFER may be also nil, meaning that this process is not associated\n\
  1375.  with any buffer\n\
  1376. Third arg is name of the host to connect to.\n\
  1377. Fourth arg SERVICE is name of the service desired, or an integer\n\
  1378.  specifying a port number to connect to.")
  1379.    (name, buffer, host, service)
  1380.       Lisp_Object name, buffer, host, service;
  1381. {
  1382.   Lisp_Object proc;
  1383.   register int i;
  1384.   struct sockaddr_in address;
  1385.   struct servent *svc_info;
  1386.   struct hostent *host_info;
  1387.   int s, outch, inch;
  1388.   char errstring[80];
  1389.   int port;
  1390.   struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
  1391.  
  1392.   GCPRO4 (name, buffer, host, service);
  1393.   CHECK_STRING (name, 0);
  1394.   CHECK_STRING (host, 0);
  1395.   if (XTYPE(service) == Lisp_Int)
  1396.     port = htons ((unsigned short) XINT (service));
  1397.   else
  1398.     {
  1399.       CHECK_STRING (service, 0);
  1400.       svc_info = getservbyname (XSTRING (service)->data, "tcp");
  1401.       if (svc_info == 0)
  1402.     error ("Unknown service \"%s\"", XSTRING (service)->data);
  1403.       port = svc_info->s_port;
  1404.     }
  1405.  
  1406.   host_info = gethostbyname (XSTRING (host)->data);
  1407.   if (host_info == 0)
  1408.     error ("Unknown host \"%s\"", XSTRING(host)->data);
  1409.  
  1410.   bzero (&address, sizeof address);
  1411.   bcopy (host_info->h_addr, (char *) &address.sin_addr, host_info->h_length);
  1412.   address.sin_family = host_info->h_addrtype;
  1413.   address.sin_port = port;
  1414.  
  1415.   s = socket (host_info->h_addrtype, SOCK_STREAM, 0);
  1416.   if (s < 0) 
  1417.     report_file_error ("error creating socket", Fcons (name, Qnil));
  1418.  
  1419.   /* Kernel bugs (on Ultrix at least) cause lossage (not just EINTR)
  1420.      when connect is interrupted.  So let's not let it get interrupted.  */
  1421.   if (interrupt_input)
  1422.     unrequest_sigio ();
  1423.   stop_polling ();
  1424.  
  1425.   while (1)
  1426.     {
  1427.       int value = connect (s, &address, sizeof address);
  1428.       /* Continue if successeful.  */
  1429.       if (value != -1)
  1430.     break;
  1431.       /* Report a "real" error.  */
  1432.       if (errno != EINTR)
  1433.     {
  1434.       close (s);
  1435.       error ("Host \"%s\" not responding", XSTRING (host)->data);
  1436.     }
  1437.       /* Loop around after temporary error.  */
  1438.     }
  1439.  
  1440.   if (interrupt_input)
  1441.     request_sigio ();
  1442.   start_polling ();
  1443.  
  1444.   inch = s;
  1445.   outch = dup (s);
  1446.   if (outch < 0) 
  1447.     report_file_error ("error duplicating socket", Fcons (name, Qnil));
  1448.  
  1449.   if (!NULL (buffer))
  1450.     buffer = Fget_buffer_create (buffer);
  1451.   proc = make_process (name);
  1452.  
  1453.   chan_process[inch] = proc;
  1454.  
  1455. #ifdef O_NONBLOCK
  1456.   fcntl (inch, F_SETFL, O_NONBLOCK);
  1457. #else
  1458. #ifdef O_NDELAY
  1459.   fcntl (inch, F_SETFL, O_NDELAY);
  1460. #endif
  1461. #endif
  1462.  
  1463.   XPROCESS (proc)->childp = host;
  1464.   XPROCESS (proc)->command_channel_p = Qnil;
  1465.   XPROCESS (proc)->buffer = buffer;
  1466.   XPROCESS (proc)->sentinel = Qnil;
  1467.   XPROCESS (proc)->filter = Qnil;
  1468.   XPROCESS (proc)->command = Qnil;
  1469.   XPROCESS (proc)->pid = Qnil;
  1470.   XPROCESS (proc)->kill_without_query = Qt;
  1471.   XFASTINT (XPROCESS (proc)->infd) = s;
  1472.   XFASTINT (XPROCESS (proc)->outfd) = outch;
  1473.   XPROCESS (proc)->status = Qrun;
  1474.   FD_SET (inch, &input_wait_mask);
  1475.  
  1476.   UNGCPRO;
  1477.   return proc;
  1478. }
  1479. #endif    /* HAVE_SOCKETS */
  1480.  
  1481. deactivate_process (proc)
  1482.      Lisp_Object proc;
  1483. {
  1484.   register int inchannel, outchannel;
  1485.   register struct Lisp_Process *p = XPROCESS (proc);
  1486.  
  1487.   inchannel = XFASTINT (p->infd);
  1488.   outchannel = XFASTINT (p->outfd);
  1489.  
  1490.   if (inchannel)
  1491.     {
  1492.       /* Beware SIGCHLD hereabouts. */
  1493.       flush_pending_output (inchannel);
  1494.       close (inchannel);
  1495.       if (outchannel  &&  outchannel != inchannel)
  1496.      close (outchannel);
  1497.  
  1498.       XFASTINT (p->infd) = 0;
  1499.       XFASTINT (p->outfd) = 0;
  1500.       chan_process[inchannel] = Qnil;
  1501.       FD_CLR (inchannel, &input_wait_mask);
  1502.     }
  1503. }
  1504.  
  1505. /* Close all descriptors currently in use for communication
  1506.    with subprocess.  This is used in a newly-forked subprocess
  1507.    to get rid of irrelevant descriptors.  */
  1508.  
  1509. close_process_descs ()
  1510. {
  1511.   int i;
  1512.   for (i = 0; i < MAXDESC; i++)
  1513.     {
  1514.       Lisp_Object process;
  1515.       process = chan_process[i];
  1516.       if (!NULL (process))
  1517.     {
  1518.       int in = XFASTINT (XPROCESS (process)->infd);
  1519.       int out = XFASTINT (XPROCESS (process)->outfd);
  1520.  
  1521.       if (in != 0)
  1522.         close (in);
  1523.       if (out != 0 && out != in)
  1524.         close (out);
  1525.       if (!NULL (XPROCESS (process)->subtty))
  1526.         close (XFASTINT (XPROCESS (process)->subtty));
  1527.     }
  1528.     }
  1529. }
  1530.  
  1531. DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
  1532.   0, 1, 0,
  1533.   "Allow any pending output from subprocesses to be read by Emacs.\n\
  1534. It is read into the process' buffers or given to their filter functions.\n\
  1535. Non-nil arg PROCESS means do not return until some output has been received\n\
  1536. from PROCESS.")
  1537.   (proc)
  1538.      register Lisp_Object proc;
  1539. {
  1540.   if (NULL (proc))
  1541.     wait_reading_process_input (-1, 0, 0);
  1542.   else
  1543.     {
  1544.       proc = get_process (proc);
  1545.       wait_reading_process_input (0, XPROCESS (proc), 0);
  1546.     }
  1547.   return Qnil;
  1548. }
  1549.  
  1550. /* This variable is different from waiting_for_input in keyboard.c.
  1551.    It is used to communicate to a lisp process-filter/sentinel (via the
  1552.    function Fwaiting_for_user_input_p below) whether emacs was waiting
  1553.    for user-input when that process-filter was called.
  1554.    waiting_for_input cannot be used as that is by definition 0 when
  1555.    lisp code is being evalled */
  1556. static int waiting_for_user_input_p;
  1557.  
  1558. /* Read and dispose of subprocess output
  1559.  while waiting for timeout to elapse and/or keyboard input to be available.
  1560.  
  1561.  time_limit is the timeout in seconds, or zero for no limit.
  1562.  -1 means gobble data available immediately but don't wait for any.
  1563.  
  1564.  read_kbd is 1 to return when input is available.
  1565.  -1 means caller will actually read the input.
  1566.  A pointer to a struct Lisp_Process means wait until
  1567.  something arrives from that process.
  1568.  
  1569.  do_display means redisplay should be done to show
  1570.  subprocess output that arrives.  */
  1571.  
  1572. wait_reading_process_input (time_limit, read_kbd, do_display)
  1573.      int time_limit, read_kbd, do_display;
  1574. {
  1575.   register int channel, nfds, m;
  1576.   SELECT_TYPE Available;
  1577.   SELECT_TYPE Exception;
  1578.   int xerrno;
  1579.   Lisp_Object proc;
  1580. #ifdef HAVE_TIMEVAL
  1581.   struct timeval timeout, end_time, garbage;
  1582. #else
  1583.   long timeout, end_time, temp;
  1584. #endif /* not HAVE_TIMEVAL */
  1585.   SELECT_TYPE Atemp;
  1586.   int wait_channel = 0;
  1587.   struct Lisp_Process *wait_proc = 0;
  1588.   extern kbd_count;
  1589.  
  1590.   /* Detect when read_kbd is really the address of a Lisp_Process.  */
  1591.   if (read_kbd > 10 || read_kbd < -1)
  1592.     {
  1593.       wait_proc = (struct Lisp_Process *) read_kbd;
  1594.       wait_channel = XFASTINT (wait_proc->infd);
  1595.       read_kbd = 0;
  1596.     }
  1597.   waiting_for_user_input_p = read_kbd;
  1598.  
  1599.   /* Since we may need to wait several times,
  1600.      compute the absolute time to return at.  */
  1601.   if (time_limit)
  1602.     {
  1603. #ifdef HAVE_TIMEVAL
  1604.       gettimeofday (&end_time, &garbage);
  1605.       end_time.tv_sec += time_limit;
  1606. #else /* not HAVE_TIMEVAL */
  1607.       time (&end_time);
  1608.       end_time += time_limit;
  1609. #endif /* not HAVE_TIMEVAL */
  1610.     }
  1611.  
  1612. #if 0  /* Select emulator claims to preserve alarms.
  1613.       And there are many ways to get out of this function by longjmp.  */
  1614.   /* Turn off periodic alarms (in case they are in use)
  1615.      because the select emulator uses alarms.  */
  1616.   stop_polling ();
  1617. #endif
  1618.  
  1619.   while (1)
  1620.     {
  1621.       /* If calling from keyboard input, do not quit
  1622.      since we want to return C-g as an input character.
  1623.      Otherwise, do pending quit if requested.  */
  1624.       if (read_kbd >= 0)
  1625.     {
  1626. #if 0
  1627.       /* This is the same condition tested by QUIT.
  1628.          We need to resume polling if we are going to quit.  */
  1629.       if (!NULL (Vquit_flag) && NULL (Vinhibit_quit))
  1630.         {
  1631.           start_polling ();
  1632.           QUIT;
  1633.         }
  1634. #endif
  1635.       QUIT;
  1636.     }
  1637.  
  1638.       /* If status of something has changed, and no input is available,
  1639.      notify the user of the change right away */
  1640.       if (update_tick != process_tick && do_display)
  1641.     {
  1642.       Atemp = input_wait_mask;
  1643. #ifdef HAVE_TIMEVAL
  1644.       timeout.tv_sec=0; timeout.tv_usec=0;
  1645. #else /* not HAVE_TIMEVAL */
  1646.       timeout = 0;
  1647. #endif /* not HAVE_TIMEVAL */
  1648.       if (select (MAXDESC, &Atemp, 0, 0, &timeout) <= 0)
  1649.         status_notify ();
  1650.     }
  1651.  
  1652.       /* Don't wait for output from a non-running process.  */
  1653.       if (wait_proc != 0 && !NULL (wait_proc->raw_status_low))
  1654.     update_status (wait_proc);
  1655.       if (wait_proc != 0
  1656.       && ! EQ (wait_proc->status, Qrun))
  1657.     break;
  1658.  
  1659.       if (fix_screen_hook)
  1660.     (*fix_screen_hook) ();
  1661.  
  1662.       /* Compute time from now till when time limit is up */
  1663.       /* Exit if already run out */
  1664.       if (time_limit == -1)
  1665.     {
  1666.       /* -1 specified for timeout means
  1667.          gobble output available now
  1668.          but don't wait at all. */
  1669. #ifdef HAVE_TIMEVAL
  1670.       timeout.tv_sec = 0;
  1671.       timeout.tv_usec = 0;
  1672. #else
  1673.       timeout = 0;
  1674. #endif /* not HAVE_TIMEVAL */
  1675.     }
  1676.       else if (time_limit)
  1677.     {
  1678. #ifdef HAVE_TIMEVAL
  1679.       gettimeofday (&timeout, &garbage);
  1680.  
  1681.       /* In effect, timeout = end_time - timeout.
  1682.          Break if result would be negative.  */
  1683.       if (timeval_subtract (&timeout, end_time, timeout))
  1684.         break;
  1685. #else /* not HAVE_TIMEVAL */
  1686.           time (&temp);
  1687.       timeout = end_time - temp;
  1688.       if (timeout < 0)
  1689.         break;
  1690. #endif /* not HAVE_TIMEVAL */
  1691.     }
  1692.       else
  1693.     {
  1694. #ifdef HAVE_TIMEVAL
  1695.       /* If no real timeout, loop sleeping with a big timeout
  1696.          so that input interrupt can wake us up by zeroing it  */
  1697.       timeout.tv_sec = 100;
  1698.       timeout.tv_usec = 0;
  1699. #else /* not HAVE_TIMEVAL */
  1700.           timeout = 100000;    /* 100000 recognized by the select emulator */
  1701. #endif /* not HAVE_TIMEVAL */
  1702.     }
  1703.  
  1704.       /* Cause quitting and alarm signals to take immediate action,
  1705.      and cause input available signals to zero out timeout */
  1706.       if (read_kbd < 0)
  1707.     set_waiting_for_input (&timeout);
  1708.  
  1709.       /* Wait till there is something to do */
  1710.  
  1711.       Available = Exception = input_wait_mask;
  1712.       if (!read_kbd)
  1713.     FD_CLR (0, &Available);
  1714.  
  1715.       if (read_kbd && kbd_count)
  1716.     nfds = 0;
  1717.       else
  1718.     /* Since we don't do anything abt Exceptions,
  1719.        let's notw wake up for them.  */
  1720.     nfds = select (MAXDESC, &Available, 0, 0, &timeout);
  1721. #if 0
  1722. #ifdef IBMRTAIX
  1723.     nfds = select (MAXDESC, &Available, 0, 0, &timeout);
  1724. #else
  1725. #ifdef HPUX
  1726.     nfds = select (MAXDESC, &Available, 0, 0, &timeout);
  1727. #else
  1728.     nfds = select (MAXDESC, &Available, 0, &Exception, &timeout);
  1729. #endif
  1730. #endif
  1731. #endif /* 0 */
  1732.       xerrno = errno;
  1733.  
  1734.       if (fix_screen_hook)
  1735.     (*fix_screen_hook) ();
  1736.  
  1737.       /* Make C-g and alarm signals set flags again */
  1738.       clear_waiting_for_input ();
  1739.  
  1740.       /* If we woke up due to SIGWINCH, actually change size now.  */
  1741.       if (read_kbd)
  1742.     do_pending_window_change ();
  1743.  
  1744.       if (time_limit && nfds == 0)    /* timeout elapsed */
  1745.     break;
  1746.       if (nfds < 0)
  1747.     {
  1748.       if (xerrno == EINTR)
  1749.         FD_ZERO (&Available);
  1750. #ifdef ALLIANT
  1751.       /* This happens for no known reason on ALLIANT.
  1752.          I am guessing that this is the right response. -- RMS.  */
  1753.       else if (xerrno == EFAULT)
  1754.         FD_ZERO (&Available);
  1755. #endif
  1756.       else if (xerrno == EBADF)
  1757. #ifdef AIX
  1758.       /* AIX will return EBADF on a call to select involving a ptc if the
  1759.          associated pts isn't open.  Since this will only happen just as
  1760.          a child is dying, just ignore the situation -- SIGCHLD will come
  1761.          along quite quickly, and after cleanup the ptc will no longer be
  1762.          checked, so this error will stop recurring.  */
  1763.         FD_ZERO (&Available);     /* Cannot depend on values returned.  */
  1764. #else /* not AIX */
  1765.         abort ();
  1766. #endif /* not AIX */
  1767.       else
  1768.         error("select error: %s", sys_errlist[xerrno]);
  1769.     }
  1770. #if defined (sun) || defined (APOLLO)
  1771.       else if (nfds > 0 && FD_ISSET (0, &Available) && interrupt_input)
  1772.     /* System sometimes fails to deliver SIGIO.  */
  1773.     kill (getpid (), SIGIO);
  1774. #endif
  1775.  
  1776.       /* Check for keyboard input */
  1777.       /* If there is any, return immediately
  1778.      to give it higher priority than subprocesses */
  1779.  
  1780.       if (read_kbd && detect_input_pending ())
  1781.     break;
  1782.  
  1783.       /* If checking input just got us a size-change event from X,
  1784.      obey it now if we should.  */
  1785.       if (read_kbd)
  1786.     do_pending_window_change ();
  1787.  
  1788.       /* If screen size has changed, redisplay now
  1789.      for either sit-for or keyboard input.  */
  1790.       if (read_kbd && screen_garbaged)
  1791.     redisplay_preserve_echo_area ();
  1792.  
  1793. #ifdef vipc
  1794.       /* Check for connection from other process */
  1795.  
  1796.       if (FD_ISSET (comm_server, &Available))
  1797.     {
  1798.       FD_CLR (comm_server, &Available);
  1799.       create_commchan ();
  1800.     }
  1801. #endif /* vipc */
  1802.  
  1803.       /* Check for data from a process or a command channel */
  1804.  
  1805.       for (channel = 3; channel < MAXDESC; channel++)
  1806.     {
  1807.       if (FD_ISSET (channel, &Available))
  1808.         {
  1809.           int nread;
  1810.  
  1811.           FD_CLR (channel, &Available);
  1812.           /* If waiting for this channel,
  1813.          arrange to return as soon as no more input
  1814.          to be processed.  No more waiting.  */
  1815.           if (wait_channel == channel)
  1816.         {
  1817.           wait_channel = 0;
  1818.           time_limit = -1;
  1819.         }
  1820.           proc = chan_process[channel];
  1821.           if (NULL (proc))
  1822.         continue;
  1823.  
  1824. #ifdef vipc
  1825.           /* It's a command channel */
  1826.           if (!NULL (XPROCESS (proc)->command_channel_p))
  1827.         {
  1828.           ProcessCommChan (channel, proc);
  1829.           if (NULL (XPROCESS (proc)->command_channel_p))
  1830.             {
  1831.               /* It has ceased to be a command channel! */
  1832.               int bytes_available;
  1833.               if (ioctl (channel, FIONREAD, &bytes_available) < 0)
  1834.             bytes_available = 0;
  1835.               if (bytes_available)
  1836.             FD_SET (channel, &Available);
  1837.             }
  1838.           continue;
  1839.         }
  1840. #endif /* vipc */
  1841.  
  1842.           /* Read data from the process, starting with our
  1843.          buffered-ahead character if we have one.  */
  1844.  
  1845.           nread = read_process_output (proc, channel);
  1846.           if (nread > 0)
  1847.         {
  1848.           /* Since read_process_output can run a filter,
  1849.              which can call accept-process-output,
  1850.              don't try to read from any other processes
  1851.              before doing the select again.  */
  1852.           FD_ZERO (&Available);
  1853.  
  1854.           if (do_display)
  1855.             redisplay_preserve_echo_area ();
  1856.         }
  1857. #ifdef EWOULDBLOCK
  1858.           else if (nread == -1 && errno == EWOULDBLOCK)
  1859.         ;
  1860. #else
  1861. #ifdef O_NONBLOCK
  1862.           else if (nread == -1 && errno == EAGAIN)
  1863.         ;
  1864. #else
  1865. #ifdef O_NDELAY
  1866.           else if (nread == -1 && errno == EAGAIN)
  1867.         ;
  1868.           /* Note that we cannot distinguish between no input
  1869.          available now and a closed pipe.
  1870.          With luck, a closed pipe will be accompanied by
  1871.          subprocess termination and SIGCHLD.  */
  1872.           else if (nread == 0 && !NETCONN_P (proc))
  1873.         ;
  1874. #endif /* O_NDELAY */
  1875. #endif /* O_NONBLOCK */
  1876. #endif /* EWOULDBLOCK */
  1877. #ifdef HAVE_PTYS
  1878.           /* On some OSs with ptys, when the process on one end of
  1879.          a pty exits, the other end gets an error reading with
  1880.          errno = EIO instead of getting an EOF (0 bytes read).
  1881.          Therefore, if we get an error reading and errno =
  1882.          EIO, just continue, because the child process has
  1883.          exited and should clean itself up soon (e.g. when we
  1884.          get a SIGCHLD). */
  1885.           else if (nread == -1 && errno == EIO && !NETCONN_P (proc))
  1886.         ;
  1887. #endif /* HAVE_PTYS */
  1888. /* If we can detect process termination, don't consider the process
  1889.    gone just because its pipe is closed.  */
  1890. #ifdef SIGCHLD
  1891.           else if (nread == 0 && !NETCONN_P (proc))
  1892.         ;
  1893. #endif
  1894.           else
  1895.         {
  1896.           /* Preserve status of processes already terminated.  */
  1897.           XSETINT (XPROCESS (proc)->tick, ++process_tick);
  1898.           deactivate_process (proc);
  1899.           if (!NULL (XPROCESS (proc)->raw_status_low))
  1900.             update_status (XPROCESS (proc));
  1901.           if (EQ (XPROCESS (proc)->status, Qrun))
  1902.             XPROCESS (proc)->status
  1903.               = Fcons (Qexit, Fcons (make_number (256), Qnil));
  1904.         }
  1905.         }
  1906.     } /* end for */
  1907.     } /* end while */
  1908.  
  1909. #if 0
  1910.   /* Resume periodic signals to poll for input, if necessary.  */
  1911.   start_polling ();
  1912. #endif
  1913. }
  1914.  
  1915. /* Actually call the filter.  This gets the information via variables
  1916.    because internal_condition_case won't pass arguments.  */
  1917.  
  1918. Lisp_Object
  1919. run_filter ()
  1920. {
  1921.   return call2 (this_filter, filter_process, filter_string);
  1922. }
  1923.  
  1924. /* Read pending output from the process channel,
  1925.    starting with our buffered-ahead character if we have one.
  1926.    Yield number of characters read.
  1927.  
  1928.    This function reads at most 1024 characters.
  1929.    If you want to read all available subprocess output,
  1930.    you must call it repeatedly until it returns zero.  */
  1931.  
  1932. read_process_output (proc, channel)
  1933.      Lisp_Object proc;
  1934.      register int channel;
  1935. {
  1936.   register int nchars;
  1937.   char chars[1024];
  1938.   register Lisp_Object outstream;
  1939.   register struct buffer *old = current_buffer;
  1940.   register struct Lisp_Process *p = XPROCESS (proc);
  1941.   register int opoint;
  1942.  
  1943.   if (proc_buffered_char[channel] < 0)
  1944.     nchars = read (channel, chars, sizeof chars);
  1945.   else
  1946.     {
  1947.       chars[0] = proc_buffered_char[channel];
  1948.       proc_buffered_char[channel] = -1;
  1949.       nchars = read (channel, chars + 1, sizeof chars - 1);
  1950.       if (nchars < 0)
  1951.     nchars = 1;
  1952.       else
  1953.     nchars = nchars + 1;
  1954.     }
  1955.  
  1956.   if (nchars <= 0) return nchars;
  1957.  
  1958.   outstream = p->filter;
  1959.   if (!NULL (outstream))
  1960.     {
  1961.       int count = specpdl_ptr - specpdl;
  1962.       specbind (Qinhibit_quit, Qt);
  1963.       this_filter = outstream;
  1964.       filter_process = proc;
  1965.       filter_string = make_string (chars, nchars);
  1966.       call2 (this_filter, filter_process, filter_string);
  1967.       /*   internal_condition_case (run_filter, Qerror, Fidentity);  */
  1968.       unbind_to (count);
  1969.       return nchars;
  1970.     }
  1971.  
  1972.   /* If no filter, write into buffer if it isn't dead.  */
  1973.   if (!NULL (p->buffer) && !NULL (XBUFFER (p->buffer)->name))
  1974.     {
  1975.       Lisp_Object tem;
  1976.  
  1977.       Fset_buffer (p->buffer);
  1978.       opoint = point;
  1979.  
  1980.       /* Insert new output into buffer
  1981.      at the current end-of-output marker,
  1982.      thus preserving logical ordering of input and output.  */
  1983.       if (XMARKER (p->mark)->buffer)
  1984.     SET_PT (marker_position (p->mark));
  1985.       else
  1986.     SET_PT (ZV);
  1987.       if (point <= opoint)
  1988.     opoint += nchars;
  1989.  
  1990.       tem = current_buffer->read_only;
  1991.       current_buffer->read_only = Qnil;
  1992.       insert (chars, nchars);
  1993.       current_buffer->read_only = tem;
  1994.       Fset_marker (p->mark, make_number (point), p->buffer);
  1995.       update_mode_lines++;
  1996.  
  1997.       SET_PT (opoint);
  1998.       set_buffer_internal (old);
  1999.     }
  2000.   return nchars;
  2001. }
  2002.  
  2003. DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p, Swaiting_for_user_input_p,
  2004.        0, 0, 0,
  2005.   "Returns non-NIL if emacs is waiting for input from the user.\n\
  2006. This is intended for use by asynchronous process output filters and sentinels.")
  2007.        ()
  2008. {
  2009.   return ((waiting_for_user_input_p) ? Qt : Qnil);
  2010. }
  2011.  
  2012. /* Sending data to subprocess */
  2013.  
  2014. jmp_buf send_process_frame;
  2015.  
  2016. send_process_trap ()
  2017. {
  2018. #ifdef BSD4_1
  2019.   sigrelse (SIGPIPE);
  2020.   sigrelse (SIGALRM);
  2021. #endif /* BSD4_1 */
  2022.   longjmp (send_process_frame, 1);
  2023. }
  2024.  
  2025. send_process (proc, buf, len)
  2026.      Lisp_Object proc;
  2027.      char *buf;
  2028.      int len;
  2029. {
  2030.   /* Don't use register vars; longjmp can lose them.  */
  2031.   int rv;
  2032.   unsigned char *procname = XSTRING (XPROCESS (proc)->name)->data;
  2033.  
  2034.   if (!NULL (XPROCESS (proc)->raw_status_low))
  2035.     update_status (XPROCESS (proc));
  2036.   if (! EQ (XPROCESS (proc)->status, Qrun))
  2037.     error ("Process %s not running", procname);
  2038.  
  2039.   if (!setjmp (send_process_frame))
  2040.     while (len > 0)
  2041.       {
  2042.     signal (SIGPIPE, send_process_trap);
  2043.     rv = write (XFASTINT (XPROCESS (proc)->outfd), buf, len);
  2044.     signal (SIGPIPE, SIG_DFL);
  2045.     if (rv < 0)
  2046.       {
  2047.         if (0
  2048. #ifdef EWOULDBLOCK
  2049.         || errno == EWOULDBLOCK
  2050. #endif
  2051. #ifdef EAGAIN
  2052.         || errno == EAGAIN
  2053. #endif
  2054.         )
  2055.           {
  2056.         /* It would be nice to accept process output here,
  2057.            but that is difficult.  For example, it could
  2058.            garbage what we are sending if that is from a buffer.  */
  2059.         immediate_quit = 1;
  2060.         QUIT;
  2061.         sleep (1);
  2062.         immediate_quit = 0;
  2063.         continue;
  2064.           }
  2065.         report_file_error ("writing to process", Fcons (proc, Qnil));
  2066.       }
  2067.     buf += rv;
  2068.     len -= rv;
  2069.       }
  2070.   else
  2071.     {
  2072.       XPROCESS (proc)->raw_status_low = Qnil;
  2073.       XPROCESS (proc)->raw_status_high = Qnil;
  2074.       XPROCESS (proc)->status = Fcons (Qexit, Fcons (make_number (256), Qnil));
  2075.       XSETINT (XPROCESS (proc)->tick, ++process_tick);
  2076.       deactivate_process (proc);
  2077.       error ("SIGPIPE raised on process %s; closed it", procname);
  2078.     }
  2079. }
  2080.  
  2081. DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
  2082.   3, 3, 0,
  2083.   "Send current contents of region as input to PROCESS.\n\
  2084. PROCESS may be a process name.\n\
  2085. Called from program, takes three arguments, PROCESS, START and END.")
  2086.   (process, start, end)
  2087.      Lisp_Object process, start, end;
  2088. {
  2089.   Lisp_Object proc;
  2090.   int start1;
  2091.  
  2092.   proc = get_process (process);
  2093.   validate_region (&start, &end);
  2094.  
  2095.   if (XINT (start) < GPT && XINT (end) > GPT)
  2096.     move_gap (start);
  2097.  
  2098.   start1 = XINT (start);
  2099.   send_process (proc, &FETCH_CHAR (start1), XINT (end) - XINT (start));
  2100.  
  2101.   return Qnil;
  2102. }
  2103.  
  2104. DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
  2105.   2, 2, 0,
  2106.   "Send PROCESS the contents of STRING as input.\n\
  2107. PROCESS may be a process name.")
  2108.   (process, string)
  2109.      Lisp_Object process, string;
  2110. {
  2111.   Lisp_Object proc;
  2112.   CHECK_STRING (string, 1);
  2113.   proc = get_process (process);
  2114.   send_process (proc, XSTRING (string)->data, XSTRING (string)->size);
  2115.   return Qnil;
  2116. }
  2117.  
  2118. /* send a signal number SIGNO to PROCESS.
  2119.    CURRENT_GROUP means send to the process group that currently owns
  2120.    the terminal being used to communicate with PROCESS.
  2121.    This is used for various commands in shell mode.
  2122.    If NOMSG is zero, insert signal-announcements into process's buffers
  2123.    right away.  */
  2124.  
  2125. process_send_signal (process, signo, current_group, nomsg)
  2126.      Lisp_Object process;
  2127.      int signo;
  2128.      Lisp_Object current_group;
  2129.      int nomsg;
  2130. {
  2131.   Lisp_Object proc;
  2132.   register struct Lisp_Process *p;
  2133.   int gid;
  2134.   int no_pgrp = 0;
  2135.  
  2136.   proc = get_process (process);
  2137.   p = XPROCESS (proc);
  2138.  
  2139.   if (!EQ (p->childp, Qt))
  2140.     error ("Process %s is not a subprocess",
  2141.        XSTRING (p->name)->data);
  2142.   if (!XFASTINT (p->infd))
  2143.     error ("Process %s is not active",
  2144.        XSTRING (p->name)->data);
  2145.  
  2146.   if (NULL (p->pty_flag))
  2147.     current_group = Qnil;
  2148.  
  2149. #ifdef TIOCGPGRP        /* Not sure about this! (fnf) */
  2150.   /* If we are using pgrps, get a pgrp number and make it negative.  */
  2151.   if (!NULL (current_group))
  2152.     {
  2153.       /* If possible, send signals to the entire pgrp
  2154.      by sending an input character to it.  */
  2155. #if defined (TIOCGLTC) && defined (TIOCGETC)
  2156.       struct tchars c;
  2157.       struct ltchars lc;
  2158.  
  2159.       switch (signo)
  2160.     {
  2161.     case SIGINT:
  2162.       ioctl (XFASTINT (p->infd), TIOCGETC, &c);
  2163.       send_process (proc, &c.t_intrc, 1);
  2164.       return Qnil;
  2165.     case SIGQUIT:
  2166.       ioctl (XFASTINT (p->infd), TIOCGETC, &c);
  2167.       send_process (proc, &c.t_quitc, 1);
  2168.       return Qnil;
  2169.     case SIGTSTP:
  2170.       ioctl (XFASTINT (p->infd), TIOCGLTC, &lc);
  2171.       send_process (proc, &lc.t_suspc, 1);
  2172.       return Qnil;
  2173.     }
  2174. #endif /* have TIOCGLTC and have TIOCGETC */
  2175.       /* It is possible that the following code would work
  2176.      on other kinds of USG systems, not just on the IRIS.
  2177.      This should be tried in Emacs 19.  */
  2178. #if defined (IRIS) && defined (HAVE_SETSID) /* Check for Irix, not older
  2179.                            systems.  */
  2180.       struct termio t;
  2181.       switch (signo)
  2182.     {
  2183.     case SIGINT:
  2184.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2185.       send_process (proc, &t.c_cc[VINTR], 1);
  2186.       return Qnil;
  2187.     case SIGQUIT:
  2188.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2189.       send_process (proc, &t.c_cc[VQUIT], 1);
  2190.       return Qnil;
  2191.     case SIGTSTP:
  2192.       ioctl (XFASTINT (p->infd), TCGETA, &t);
  2193.       send_process (proc, &t.c_cc[VSWTCH], 1);
  2194.       return Qnil;
  2195.     }
  2196. #endif /* IRIS and HAVE_SETSID */
  2197.  
  2198.       /* Get the pgrp using the tty itself, if we have that.
  2199.      Otherwise, use the pty to get the pgrp.  */
  2200.       if (!NULL (p->subtty))
  2201.     ioctl (XFASTINT (p->subtty), TIOCGPGRP, &gid);
  2202.       else
  2203.     ioctl (XFASTINT (p->infd), TIOCGPGRP, &gid);
  2204.       if (gid == -1)
  2205.     no_pgrp = 1;
  2206.       else
  2207.     gid = - gid;
  2208.     }
  2209.   else
  2210.     gid = - XFASTINT (p->pid);
  2211. #else /* not using pgrps */
  2212.   /* Can't select pgrps on this system, so we know that
  2213.      the child itself heads the pgrp.  */
  2214.   gid = - XFASTINT (p->pid);
  2215. #endif /* not using pgrps */
  2216.  
  2217.   switch (signo)
  2218.     {
  2219. #ifdef SIGCONT
  2220.     case SIGCONT:
  2221.       p->raw_status_low = Qnil;
  2222.       p->raw_status_high = Qnil;
  2223.       p->status = Qrun;
  2224.       XSETINT (p->tick, ++process_tick);
  2225.       if (!nomsg)
  2226.     status_notify ();
  2227.       break;
  2228. #endif
  2229.     case SIGINT:
  2230.     case SIGQUIT:
  2231.     case SIGKILL:
  2232.       flush_pending_output (XFASTINT (p->infd));
  2233.       break;
  2234.     }
  2235.  
  2236.   /* If we don't have process groups, send the signal to the immediate subprocess.
  2237.      That isn't really right, but it's better than any obvious alternative.  */
  2238.   if (no_pgrp)
  2239.     {
  2240.       kill (XFASTINT (p->pid), signo);
  2241.       return;
  2242.     }
  2243.  
  2244.   /* gid may be a pid, or minus a pgrp's number */
  2245. #ifdef TIOCSIGSEND
  2246.   if (!NULL (current_group))
  2247.     ioctl (XFASTINT (p->infd), TIOCSIGSEND, signo);
  2248.   else
  2249.     {
  2250.       gid = - XFASTINT (p->pid);
  2251.       kill (gid, signo);
  2252.     }
  2253. #else /* no TIOCSIGSEND */
  2254. #ifdef BSD
  2255.   /* On bsd, [man says] kill does not accept a negative number to kill a pgrp.
  2256.      Must do that differently.  */
  2257.   killpg (-gid, signo);
  2258. #else /* Not BSD.  */
  2259.   kill (gid, signo);
  2260. #endif /* Not BSD.  */
  2261. #endif /* no TIOCSIGSEND */
  2262. }
  2263.  
  2264. DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
  2265.   "Interrupt process PROCESS.  May be process or name of one.\n\
  2266. Nil or no arg means current buffer's process.\n\
  2267. Second arg CURRENT-GROUP non-nil means send signal to\n\
  2268. the current process-group of the process's controlling terminal\n\
  2269. rather than to the process's own process group.\n\
  2270. If the process is a shell, this means interrupt current subjob\n\
  2271. rather than the shell.")
  2272.   (process, current_group)
  2273.      Lisp_Object process, current_group;
  2274. {
  2275.   process_send_signal (process, SIGINT, current_group, 0);
  2276.   return process;
  2277. }
  2278.  
  2279. DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
  2280.   "Kill process PROCESS.  May be process or name of one.\n\
  2281. See function interrupt-process for more details on usage.")
  2282.   (process, current_group)
  2283.      Lisp_Object process, current_group;
  2284. {
  2285.   process_send_signal (process, SIGKILL, current_group, 0);
  2286.   return process;
  2287. }
  2288.  
  2289. DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
  2290.   "Send QUIT signal to process PROCESS.  May be process or name of one.\n\
  2291. See function interrupt-process for more details on usage.")
  2292.   (process, current_group)
  2293.      Lisp_Object process, current_group;
  2294. {
  2295.   process_send_signal (process, SIGQUIT, current_group, 0);
  2296.   return process;
  2297. }
  2298.  
  2299. DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
  2300.   "Stop process PROCESS.  May be process or name of one.\n\
  2301. See function interrupt-process for more details on usage.")
  2302.   (process, current_group)
  2303.      Lisp_Object process, current_group;
  2304. {
  2305. #ifndef SIGTSTP
  2306.   error ("no SIGTSTP support");
  2307. #else
  2308.   process_send_signal (process, SIGTSTP, current_group, 0);
  2309. #endif
  2310.   return process;
  2311. }
  2312.  
  2313. DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
  2314.   "Continue process PROCESS.  May be process or name of one.\n\
  2315. See function interrupt-process for more details on usage.")
  2316.   (process, current_group)
  2317.      Lisp_Object process, current_group;
  2318. {
  2319. #ifdef SIGCONT
  2320.     process_send_signal (process, SIGCONT, current_group, 0);
  2321. #else
  2322.     error ("no SIGCONT support");
  2323. #endif
  2324.   return process;
  2325. }
  2326.  
  2327. DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
  2328.   "Make PROCESS see end-of-file in its input.\n\
  2329. Eof comes after any text already sent to it.\n\
  2330. nil or no arg means current buffer's process.")
  2331.   (process)
  2332.      Lisp_Object process;
  2333. {
  2334.   Lisp_Object proc;
  2335.  
  2336.   proc = get_process (process);
  2337.   /* Sending a zero-length record is supposed to mean eof
  2338.      when TIOCREMOTE is turned on.  */
  2339. #ifdef DID_REMOTE
  2340.   {
  2341.     char buf[1];
  2342.     write (XFASTINT (XPROCESS (proc)->outfd), buf, 0);
  2343.   }
  2344. #else /* did not do TOICREMOTE */
  2345.   if (!NULL (XPROCESS (proc)->pty_flag))
  2346.     send_process (proc, "\004", 1);
  2347.   else
  2348.     {
  2349.       close (XPROCESS (proc)->outfd);
  2350.       XFASTINT (XPROCESS (proc)->outfd) = open ("/dev/null", O_WRONLY);
  2351.     }
  2352.  
  2353. #endif /* did not do TOICREMOTE */
  2354.   return process;
  2355. }
  2356.  
  2357. /* Kill all processes associated with `buffer'.
  2358.  If `buffer' is nil, kill all processes  */
  2359.  
  2360. kill_buffer_processes (buffer)
  2361.      Lisp_Object buffer;
  2362. {
  2363.   Lisp_Object tail, proc;
  2364.  
  2365.   for (tail = Vprocess_alist; XGCTYPE (tail) == Lisp_Cons;
  2366.        tail = XCONS (tail)->cdr)
  2367.     {
  2368.       proc = XCONS (XCONS (tail)->car)->cdr;
  2369.       if (XGCTYPE (proc) == Lisp_Process
  2370.       && (NULL (buffer) || EQ (XPROCESS (proc)->buffer, buffer)))
  2371.     {
  2372.       if (NETCONN_P (proc))
  2373.         deactivate_process (proc);
  2374.       else if (XFASTINT (XPROCESS (proc)->infd))
  2375.         process_send_signal (proc, SIGHUP, Qnil, 1);
  2376.     }
  2377.     }
  2378. }
  2379.  
  2380. /* On receipt of a signal that a child status has changed,
  2381.  loop asking about children with changed statuses until
  2382.  the system says there are no more.
  2383.    All we do is change the status;
  2384.  we do not run sentinels or print notifications.
  2385.  That is saved for the next time keyboard input is done,
  2386.  in order to avoid timing errors.  */
  2387.  
  2388. /** WARNING: this can be called during garbage collection.
  2389.  Therefore, it must not be fooled by the presence of mark bits in
  2390.  Lisp objects.  */
  2391.  
  2392. /** USG WARNING:  Although it is not obvious from the documentation
  2393.  in signal(2), on a USG system the SIGCLD handler MUST NOT call
  2394.  signal() before executing at least one wait(), otherwise the handler
  2395.  will be called again, resulting in an infinite loop.  The relevant
  2396.  portion of the documentation reads "SIGCLD signals will be queued
  2397.  and the signal-catching function will be continually reentered until
  2398.  the queue is empty".  Invoking signal() causes the kernel to reexamine
  2399.  the SIGCLD queue.   Fred Fish, UniSoft Systems Inc. */
  2400.  
  2401. sigchld_handler (signo)
  2402.      int signo;
  2403. {
  2404.   int old_errno = errno;
  2405.   Lisp_Object proc;
  2406.   register struct Lisp_Process *p;
  2407.  
  2408. #ifdef BSD4_1
  2409.   extern int synch_process_pid;
  2410.   extern int sigheld;
  2411.   sigheld |= sigbit (SIGCHLD);
  2412. #endif
  2413.  
  2414.   while (1)
  2415.     {
  2416.       register int pid;
  2417.       WAITTYPE w;
  2418.       Lisp_Object tail;
  2419.  
  2420. #ifdef WNOHANG
  2421. #ifndef WUNTRACED
  2422. #define WUNTRACED 0
  2423. #endif /* no WUNTRACED */
  2424.       /* Keep trying to get a status until we get a definitive result.  */
  2425.       do 
  2426.     {
  2427.       errno = 0;
  2428.       pid = wait3 (&w, WNOHANG | WUNTRACED, 0);
  2429.     }
  2430.       while (pid <= 0 && errno == EINTR);
  2431.  
  2432.       if (pid <= 0)
  2433.     {
  2434.       /* A real failure.  We have done all our job, so return.  */
  2435.  
  2436.       /* USG systems forget handlers when they are used;
  2437.          must reestablish each time */
  2438. #ifdef USG
  2439.       signal (signo, sigchld_handler);   /* WARNING - must come after wait3() */
  2440. #endif
  2441. #ifdef  BSD4_1
  2442.       sigheld &= ~sigbit (SIGCHLD);
  2443.       sigrelse (SIGCHLD);
  2444. #endif
  2445.       errno = old_errno;
  2446.       return;
  2447.     }
  2448. #else
  2449.       pid = wait (&w);
  2450. #endif /* no WNOHANG */
  2451.  
  2452. #ifdef BSD4_1
  2453.       if (synch_process_pid == pid)
  2454.     synch_process_pid = 0;         /* Zero it to show process has died. */
  2455. #endif
  2456.  
  2457.       /* Find the process that signaled us, and record its status.  */
  2458.  
  2459.       p = 0;
  2460.       for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
  2461.     {
  2462.       proc = XCONS (XCONS (tail)->car)->cdr;
  2463.       p = XPROCESS (proc);
  2464.       if (EQ (p->childp, Qt) && XFASTINT (p->pid) == pid)
  2465.         break;
  2466.       p = 0;
  2467.     }
  2468.  
  2469.       /* If we don't recognize the pid number,
  2470.      look for a process being created.  */
  2471.  
  2472.       if (p == 0)
  2473.     for (tail = Vprocess_alist; XSYMBOL (tail) != XSYMBOL (Qnil); tail = XCONS (tail)->cdr)
  2474.       {
  2475.         proc = XCONS (XCONS (tail)->car)->cdr;
  2476.         p = XPROCESS (proc);
  2477.         if (XINT (p->pid) == -1)
  2478.           break;
  2479.         p = 0;
  2480.       }
  2481.  
  2482.       /* Change the status of the process that was found.  */
  2483.  
  2484.       if (p != 0)
  2485.     {
  2486.       union { int i; WAITTYPE wt; } u;
  2487.  
  2488.       XSETINT (p->tick, ++process_tick);
  2489.       u.wt = w;
  2490.       XFASTINT (p->raw_status_low) = u.i & 0xffff;
  2491.       XFASTINT (p->raw_status_high) = u.i >> 16;
  2492.  
  2493.       /* If process has terminated, stop waiting for its output.  */
  2494.       if (WIFSIGNALED (w) || WIFEXITED (w))
  2495.         if (p->infd)
  2496.           FD_CLR (p->infd, &input_wait_mask);
  2497.     }
  2498.       else
  2499.     {
  2500.       /* Report the status of the synchronous process.  */
  2501.       if (WIFEXITED (w))
  2502.         synch_process_retcode = WRETCODE (w);
  2503.       else if (WIFSIGNALED (w))
  2504.         synch_process_death = sys_siglist[WTERMSIG (w)];
  2505.     }
  2506.  
  2507.       /* On some systems, we must return right away.
  2508.      If any more processes want to signal us, we will
  2509.      get another signal.
  2510.      Otherwise (on systems that have WNOHANG), loop around
  2511.      to use up all the processes that have something to tell us.  */
  2512. #if defined (USG) && ! (defined (HPUX) && defined (WNOHANG))
  2513. #ifdef USG
  2514.       signal (signo, sigchld_handler);
  2515. #endif
  2516.       errno = old_errno;
  2517.       return;
  2518. #endif /* USG, but not HPUX with WNOHANG */
  2519.     }
  2520. }
  2521.  
  2522. /* Report all recent events of a change in process status
  2523.    (either run the sentinel or output a message).
  2524.    This is done while Emacs is waiting for keyboard input.  */
  2525.  
  2526. status_notify ()
  2527. {
  2528.   register Lisp_Object proc, buffer;
  2529.   Lisp_Object tail = Qnil;
  2530.   Lisp_Object msg = Qnil;
  2531.   struct gcpro gcpro1, gcpro2;
  2532.  
  2533.   /* We need to gcpro tail; if read_process_output calls a filter
  2534.      which deletes a process and removes the cons to which tail points
  2535.      from Vprocess_alist, tail becomes an unprotected reference.  */
  2536.   GCPRO2 (tail, msg);
  2537.  
  2538.   for (tail = Vprocess_alist; !NULL (tail); tail = Fcdr (tail))
  2539.     {
  2540.       Lisp_Object symbol;
  2541.       register struct Lisp_Process *p;
  2542.  
  2543.       proc = Fcdr (Fcar (tail));
  2544.       p = XPROCESS (proc);
  2545.  
  2546.       if (XINT (p->tick) != XINT (p->update_tick))
  2547.     {
  2548.       XSETINT (p->update_tick, XINT (p->tick));
  2549.  
  2550.       /* If process is still active, read any output that remains.  */
  2551.       if (XFASTINT (p->infd))
  2552.         while (read_process_output (proc, XFASTINT (p->infd)) > 0);
  2553.  
  2554.       buffer = p->buffer;
  2555.  
  2556.       /* Get the text to use for the message.  */
  2557.       if (!NULL (p->raw_status_low))
  2558.         update_status (p);
  2559.       msg = status_message (p->status);
  2560.  
  2561.       /* If process is terminated, deactivate it or delete it.  */
  2562.       symbol = p->status;
  2563.       if (XTYPE (p->status) == Lisp_Cons)
  2564.         symbol = XCONS (p->status)->car;
  2565.  
  2566.       if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
  2567.           || EQ (symbol, Qclosed))
  2568.         {
  2569.           if (delete_exited_processes)
  2570.         remove_process (proc);
  2571.           else
  2572.         deactivate_process (proc);
  2573.         }
  2574.  
  2575.       /* Now output the message suitably.  */
  2576.       if (!NULL (p->sentinel))
  2577.         exec_sentinel (proc, msg);
  2578.       /* Don't bother with a message in the buffer
  2579.          when a process becomes runnable.  */
  2580.       else if (!EQ (symbol, Qrun) && !NULL (buffer))
  2581.         {
  2582.           Lisp_Object ro = XBUFFER (buffer)->read_only;
  2583.           Lisp_Object tem;
  2584.           struct buffer *old = current_buffer;
  2585.           int opoint;
  2586.  
  2587.           /* Avoid error if buffer is deleted
  2588.          (probably that's why the process is dead, too) */
  2589.           if (NULL (XBUFFER (buffer)->name))
  2590.         continue;
  2591.           Fset_buffer (buffer);
  2592.           opoint = point;
  2593.           /* Insert new output into buffer
  2594.          at the current end-of-output marker,
  2595.          thus preserving logical ordering of input and output.  */
  2596.           if (XMARKER (p->mark)->buffer)
  2597.         SET_PT (marker_position (p->mark));
  2598.           else
  2599.         SET_PT (ZV);
  2600.           if (point <= opoint)
  2601.         opoint += XSTRING (msg)->size + XSTRING (p->name)->size + 10;
  2602.  
  2603.           tem = current_buffer->read_only;
  2604.           current_buffer->read_only = Qnil;
  2605.           InsStr ("\nProcess ");
  2606.           Finsert (1, &p->name);
  2607.           InsStr (" ");
  2608.           Finsert (1, &msg);
  2609.           current_buffer->read_only = tem;
  2610.           Fset_marker (p->mark, make_number (point), p->buffer);
  2611.  
  2612.           SET_PT (opoint);
  2613.           set_buffer_internal (old);
  2614.         }
  2615.     }
  2616.     } /* end for */
  2617.  
  2618.   update_mode_lines++;  /* in case buffers use %s in mode-line-format */
  2619.   redisplay_preserve_echo_area ();
  2620.  
  2621.   update_tick = process_tick;
  2622.  
  2623.   UNGCPRO;
  2624. }
  2625.  
  2626. exec_sentinel (proc, reason)
  2627.      Lisp_Object proc, reason;
  2628. {
  2629.   Lisp_Object sentinel;
  2630.   register struct Lisp_Process *p = XPROCESS (proc);
  2631.   int count = specpdl_ptr - specpdl;
  2632.  
  2633.   sentinel = p->sentinel;
  2634.   if (NULL (sentinel))
  2635.     return;
  2636.  
  2637.   p->sentinel = Qnil;
  2638.   specbind (Qinhibit_quit, Qt);
  2639.   this_filter = sentinel;
  2640.   filter_process = proc;
  2641.   filter_string = reason;
  2642.   call2 (this_filter, filter_process, filter_string);
  2643. /*   internal_condition_case (run_filter, Qerror, Fidentity);  */
  2644.   unbind_to (count);
  2645.   p->sentinel = sentinel;
  2646. }
  2647.  
  2648. init_process ()
  2649. {
  2650.   register int i;
  2651.  
  2652. #ifdef SIGCHLD
  2653. #ifndef CANNOT_DUMP
  2654.   if (! noninteractive || initialized)
  2655. #endif
  2656.     signal (SIGCHLD, sigchld_handler);
  2657. #endif
  2658.  
  2659.   FD_ZERO (&input_wait_mask);
  2660.   FD_SET (0, &input_wait_mask);
  2661.   Vprocess_alist = Qnil;
  2662.   for (i = 0; i < MAXDESC; i++)
  2663.     {
  2664.       chan_process[i] = Qnil;
  2665.       proc_buffered_char[i] = -1;
  2666.     }
  2667. }
  2668.  
  2669. syms_of_process ()
  2670. {
  2671.   Qprocessp = intern ("processp");
  2672.   staticpro (&Qprocessp);
  2673.   Qrun = intern ("run");
  2674.   staticpro (&Qrun);
  2675.   Qstop = intern ("stop");
  2676.   staticpro (&Qstop);
  2677.   Qsignal = intern ("signal");
  2678.   staticpro (&Qsignal);
  2679.  
  2680.   /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
  2681.      here again.
  2682.  
  2683.      Qexit = intern ("exit");
  2684.      staticpro (&Qexit); */
  2685.  
  2686.   Qopen = intern ("open");
  2687.   staticpro (&Qopen);
  2688.   Qclosed = intern ("closed");
  2689.   staticpro (&Qclosed);
  2690.  
  2691.   staticpro (&Vprocess_alist);
  2692.  
  2693.   DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
  2694.     "*Non-nil means delete processes immediately when they exit.\n\
  2695. nil means don't delete them until `list-processes' is run.");
  2696.  
  2697.   delete_exited_processes = 1;
  2698.  
  2699.   DEFVAR_LISP ("process-connection-type", &Vprocess_connection_type,
  2700.     "Control type of device used to communicate with subprocesses.\n\
  2701. Values are nil to use a pipe, t for a pty (or pipe if ptys not supported).\n\
  2702. Value takes effect when `start-process' is called.");
  2703.   Vprocess_connection_type = Qt;
  2704.  
  2705.   defsubr (&Sprocessp);
  2706.   defsubr (&Sget_process);
  2707.   defsubr (&Sget_buffer_process);
  2708.   defsubr (&Sdelete_process);
  2709.   defsubr (&Sprocess_status);
  2710.   defsubr (&Sprocess_exit_status);
  2711.   defsubr (&Sprocess_id);
  2712.   defsubr (&Sprocess_name);
  2713.   defsubr (&Sprocess_command);
  2714.   defsubr (&Sset_process_buffer);
  2715.   defsubr (&Sprocess_buffer);
  2716.   defsubr (&Sprocess_mark);
  2717.   defsubr (&Sset_process_filter);
  2718.   defsubr (&Sprocess_filter);
  2719.   defsubr (&Sset_process_sentinel);
  2720.   defsubr (&Sprocess_sentinel);
  2721.   defsubr (&Sprocess_kill_without_query);
  2722.   defsubr (&Slist_processes);
  2723.   defsubr (&Sprocess_list);
  2724.   defsubr (&Sstart_process);
  2725. #ifdef HAVE_SOCKETS
  2726.   defsubr (&Sopen_network_stream);
  2727. #endif /* HAVE_SOCKETS */
  2728.   defsubr (&Saccept_process_output);
  2729.   defsubr (&Sprocess_send_region);
  2730.   defsubr (&Sprocess_send_string);
  2731.   defsubr (&Sinterrupt_process);
  2732.   defsubr (&Skill_process);
  2733.   defsubr (&Squit_process);
  2734.   defsubr (&Sstop_process);
  2735.   defsubr (&Scontinue_process);
  2736.   defsubr (&Sprocess_send_eof);
  2737.   defsubr (&Swaiting_for_user_input_p);
  2738. }
  2739.  
  2740. #endif /* subprocesses */
  2741.